## Replication: "Experimental Evidence on the Financial Consequences of International Organization Legitimacy" Part 1 ##

## Clear all
rm(list = ls(all.names = TRUE))

## Set working directory
setwd("WORKING DIRECTORY")

## Load packages
library(ggplot2)
library(dplyr)
library(tidyr)
library(tidyverse)
library(ggpubr)
library(rstatix)
library(openxlsx)


# (1) Experiment 1 

## Load data 
india1 = read.csv('aggregate_r1_CHN.csv')
india2 = read.csv('aggregate_r1_CHN.csv')

brazil1 = read.csv('aggregate_r1_CHN.csv')
brazil2 = read.csv('aggregate_r1_CHN.csv')

egypt1 = read.csv('aggregate_r1_CHN.csv')
egypt2 = read.csv('aggregate_r1_CHN.csv')

saudi1 = read.csv('aggregate_r1_CHN.csv')
saudi2 = read.csv('aggregate_r1_CHN.csv')

uk1 = read.csv('aggregate_r1_CHN.csv')
uk2 = read.csv('aggregate_r1_CHN.csv')


# India
india1$nonclick=india1$Reach-india1$Unique.link.clicks
india2$nonclick=india2$Reach-india2$Unique.link.clicks

## COMBINATION 1
india1_normlegitimacy_unicef=subset(india1, Ad.Set.Name=='Charity_India_c1_normlegitimacy_unicef')
sum(india1_normlegitimacy_unicef$Unique.link.clicks)
sum(india1_normlegitimacy_unicef$nonclick)
india1_normlegitimacy_unicefcol=rep(c(1, 0), times = c(sum(india1_normlegitimacy_unicef$Unique.link.clicks),
                                                       sum(india1_normlegitimacy_unicef$nonclick)))
india1_performancelegitimacy_unicef=subset(india1, Ad.Set.Name=="Charity_India_c1_performancelegitimacy_unicef")
sum(india1_performancelegitimacy_unicef$Unique.link.clicks)
sum(india1_performancelegitimacy_unicef$nonclick)
india1_performancelegitimacy_unicefcol=rep(c(1, 0), times = c(sum(india1_performancelegitimacy_unicef$Unique.link.clicks),
                                                              sum(india1_performancelegitimacy_unicef$nonclick)))

india1_procedurallegitimacy_unicef=subset(india1, Ad.Set.Name=="Charity_India_c1_procedurallegitimacy_unicef")
sum(india1_procedurallegitimacy_unicef$Unique.link.clicks)
sum(india1_procedurallegitimacy_unicef$nonclick)
india1_procedurallegitimacy_unicefcol=rep(c(1, 0), times = c(sum(india1_procedurallegitimacy_unicef$Unique.link.clicks),
                                                             sum(india1_procedurallegitimacy_unicef$nonclick)))

india1_placebo1_unicef=subset(india1, Ad.Set.Name=="Charity_India_c1_placebo1_unicef")
sum(india1_placebo1_unicef$Unique.link.clicks)
sum(india1_placebo1_unicef$nonclick)
india1_placebo1_unicefcol=rep(c(1, 0), times = c(sum(india1_placebo1_unicef$Unique.link.clicks),
                                                 sum(india1_placebo1_unicef$nonclick)))

## COMBINATION 2
india2_placebo1=subset(india2, Ad.Set.Name=='Charity_India_c2_placebo1_stc')
sum(india2_placebo1$Unique.link.clicks)
sum(india2_placebo1$nonclick)
india2_placebo1col=rep(c(1, 0), times = c(sum(india2_placebo1$Unique.link.clicks),
                                          sum(india2_placebo1$nonclick)))

india2_normlegitimacy_stc=subset(india2, Ad.Set.Name=='Charity_India_c2_normlegitimacy_stc')
sum(india2_normlegitimacy_stc$Unique.link.clicks)
sum(india2_normlegitimacy_stc$nonclick)
india2_normlegitimacy_stccol=rep(c(1, 0), times = c(sum(india2_normlegitimacy_stc$Unique.link.clicks), 
                                                    sum(india2_normlegitimacy_stc$nonclick)))

india2_performancelegitimacy_stc=subset(india2, Ad.Set.Name=="Charity_India_c2_performancelegitimacy_stc")
sum(india2_performancelegitimacy_stc$Unique.link.clicks)
sum(india2_performancelegitimacy_stc$nonclick)
india2_performancelegitimacy_stccol=rep(c(1, 0), times = c(sum(india2_performancelegitimacy_stc$Unique.link.clicks),
                                                           sum(india2_performancelegitimacy_stc$nonclick)))

india2_procedurallegitimacy_stc=subset(india2, Ad.Set.Name=="Charity_India_c2_procedurallegitimacy_stc")
sum(india2_procedurallegitimacy_stc$Unique.link.clicks)
sum(india2_procedurallegitimacy_stc$nonclick)
india2_procedurallegitimacy_stccol=rep(c(1, 0), times = c(sum(india2_procedurallegitimacy_stc$Unique.link.clicks),
                                                          sum(india2_procedurallegitimacy_stc$nonclick)))


## India: Create Output Table
tests = c("Norm (UNICEF)", "Performance (UNICEF)", "Procedural (UNICEF)",
          "Norm  (STC)", "Performance (STC)", "Procedural (STC)")

diff = c(mean(india1_normlegitimacy_unicefcol)-mean(india1_placebo1_unicefcol),
         mean(india1_performancelegitimacy_unicefcol)-mean(india1_placebo1_unicefcol),
         mean(india1_procedurallegitimacy_unicefcol)-mean(india1_placebo1_unicefcol),
         mean(india2_normlegitimacy_stccol)-mean(india2_placebo1col),
         mean(india2_performancelegitimacy_stccol)-mean(india2_placebo1col),
         mean(india2_procedurallegitimacy_stccol)-mean(india2_placebo1col))

mean1 = c(mean(india1_normlegitimacy_unicefcol),
          mean(india1_performancelegitimacy_unicefcol),
          mean(india1_procedurallegitimacy_unicefcol),
          mean(india2_normlegitimacy_stccol),
          mean(india2_performancelegitimacy_stccol),
          mean(india2_procedurallegitimacy_stccol))

mean2 = c(mean(india1_placebo1_unicefcol),
          mean(india1_placebo1_unicefcol),
          mean(india1_placebo1_unicefcol),
          mean(india2_placebo1col),
          mean(india2_placebo1col),
          mean(india2_placebo1col))


### Std error
std.error <- function(x) {
  sd(x) / sqrt(length(x))
}

se = c(std.error(india1_normlegitimacy_unicefcol),
       std.error(india1_performancelegitimacy_unicefcol),
       std.error(india1_procedurallegitimacy_unicefcol),
       std.error(india2_normlegitimacy_stccol),
       std.error(india2_performancelegitimacy_stccol),
       std.error(india2_procedurallegitimacy_stccol))

pvalue = c(t.test(india1_normlegitimacy_unicefcol,india1_placebo1_unicefcol)$p.value,
           t.test(india1_performancelegitimacy_unicefcol,india1_placebo1_unicefcol)$p.value,
           t.test(india1_procedurallegitimacy_unicefcol,india1_placebo1_unicefcol)$p.value,
           t.test(india2_normlegitimacy_stccol,india2_placebo1col)$p.value,
           t.test(india2_performancelegitimacy_stccol,india2_placebo1col)$p.value,
           t.test(india2_procedurallegitimacy_stccol,india2_placebo1col)$p.value)

test1 <- t.test(india1_normlegitimacy_unicefcol,india1_placebo1_unicefcol)
test2 <- t.test(india1_performancelegitimacy_unicefcol,india1_placebo1_unicefcol)
test3 <- t.test(india1_procedurallegitimacy_unicefcol,india1_placebo1_unicefcol)
test4 <- t.test(india2_normlegitimacy_stccol,india2_placebo1col)
test5 <- t.test(india2_performancelegitimacy_stccol,india2_placebo1col)
test6 <- t.test(india2_procedurallegitimacy_stccol,india2_placebo1col)

sample_size_test1 <- length(india1_normlegitimacy_unicefcol) + length(india1_placebo1_unicefcol)
sample_size_test2 <- length(india1_performancelegitimacy_unicefcol) + length(india1_placebo1_unicefcol)
sample_size_test3 <- length(india1_procedurallegitimacy_unicefcol) + length(india1_placebo1_unicefcol)
sample_size_test4 <- length(india2_normlegitimacy_stccol) + length(india2_placebo1col)
sample_size_test5 <- length(india2_performancelegitimacy_stccol) + length(india2_placebo1col)
sample_size_test6 <- length(india2_procedurallegitimacy_stccol) + length(india2_placebo1col)

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3, 
                sample_size_test4, sample_size_test5, sample_size_test6)

LowerCI = c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1], 
            test4$conf.int[1], test5$conf.int[1], test6$conf.int[1])
UpperCI = c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2], 
            test4$conf.int[2], test5$conf.int[2], test6$conf.int[2])

df_india_ab_r1 <- data.frame(country = rep(c("India"),6),
                             test = tests,
                             difference = diff,
                             lowerci = LowerCI,
                             upperci = UpperCI,
                             mean1 = mean1, 
                             mean2 = mean2,
                             p.value = pvalue,
                             se = se,
                             sample.size = sample_sizes)


# Brazil
brazil1$nonclick=brazil1$Reach-brazil1$Unique.link.clicks
brazil2$nonclick=brazil2$Reach-brazil2$Unique.link.clicks

## COMBINATION 1
brazil1_normlegitimacy_unicef=subset(brazil1, Ad.Set.Name=='charity_brazil_c1_norm_unicef')
sum(brazil1_normlegitimacy_unicef$Unique.link.clicks)
sum(brazil1_normlegitimacy_unicef$nonclick)
brazil1_normlegitimacy_unicefcol=rep(c(1, 0), times = c(sum(brazil1_normlegitimacy_unicef$Unique.link.clicks),
                                                        sum(brazil1_normlegitimacy_unicef$nonclick)))

brazil1_performancelegitimacy_unicef=subset(brazil1, Ad.Set.Name=="charity_brazil_c1_performance_unicef")
sum(brazil1_performancelegitimacy_unicef$Unique.link.clicks)
sum(brazil1_performancelegitimacy_unicef$nonclick)
brazil1_performancelegitimacy_unicefcol=rep(c(1, 0), times = c(sum(brazil1_performancelegitimacy_unicef$Unique.link.clicks),
                                                               sum(brazil1_performancelegitimacy_unicef$nonclick)))

brazil1_procedurallegitimacy_unicef=subset(brazil1, Ad.Set.Name=="charity_brazil_c1_procedural_unicef")
sum(brazil1_procedurallegitimacy_unicef$Unique.link.clicks)
sum(brazil1_procedurallegitimacy_unicef$nonclick)
brazil1_procedurallegitimacy_unicefcol=rep(c(1, 0), times = c(sum(brazil1_procedurallegitimacy_unicef$Unique.link.clicks),
                                                              sum(brazil1_procedurallegitimacy_unicef$nonclick)))

brazil1_placebo1_unicef=subset(brazil1, Ad.Set.Name=="charity_brazil_c1_placebo1")
sum(brazil1_placebo1_unicef$Unique.link.clicks)
sum(brazil1_placebo1_unicef$nonclick)
brazil1_placebo1_unicefcol=rep(c(1, 0), times = c(sum(brazil1_placebo1_unicef$Unique.link.clicks),
                                                  sum(brazil1_placebo1_unicef$nonclick)))

## COMBINATION 2
brazil2_normlegitimacy_stc=subset(brazil2, Ad.Set.Name=='charity_brazil_c2_norm_stc')
sum(brazil2_normlegitimacy_stc$Unique.link.clicks)
sum(brazil2_normlegitimacy_stc$nonclick)
brazil2_normlegitimacy_stccol=rep(c(1, 0), times = c(sum(brazil2_normlegitimacy_stc$Unique.link.clicks), 
                                                     sum(brazil2_normlegitimacy_stc$nonclick)))

brazil2_performancelegitimacy_stc=subset(brazil2, Ad.Set.Name=="charity_brazil_c2_performance_stc")
sum(brazil2_performancelegitimacy_stc$Unique.link.clicks)
sum(brazil2_performancelegitimacy_stc$nonclick)
brazil2_performancelegitimacy_stccol=rep(c(1, 0), times = c(sum(brazil2_performancelegitimacy_stc$Unique.link.clicks),
                                                            sum(brazil2_performancelegitimacy_stc$nonclick)))

brazil2_placebo1_stc=subset(brazil2, Ad.Set.Name=="charity_brazil_c2_placebo1")
sum(brazil2_placebo1_stc$Unique.link.clicks)
sum(brazil2_placebo1_stc$nonclick)
brazil2_placebo1_stccol=rep(c(1, 0), times = c(sum(brazil2_placebo1_stc$Unique.link.clicks),
                                               sum(brazil2_placebo1_stc$nonclick)))

brazil2_procedurallegitimacy_stc=subset(brazil2, Ad.Set.Name=="charity_brazil_c2_procedural_stc")
sum(brazil2_procedurallegitimacy_stc$Unique.link.clicks)
sum(brazil2_procedurallegitimacy_stc$nonclick)
brazil2_procedurallegitimacy_stccol=rep(c(1, 0), times = c(sum(brazil2_procedurallegitimacy_stc$Unique.link.clicks),
                                                           sum(brazil2_procedurallegitimacy_stc$nonclick)))


## Brazil: Create Output Table
tests = c("Norm (UNICEF)", "Performance (UNICEF)", "Procedural (UNICEF)",
          "Norm  (STC)", "Performance (STC)", "Procedural (STC)")

diff = c(mean(brazil1_normlegitimacy_unicefcol)-mean(brazil1_placebo1_unicefcol),
         mean(brazil1_performancelegitimacy_unicefcol)-mean(brazil1_placebo1_unicefcol),
         mean(brazil1_procedurallegitimacy_unicefcol)-mean(brazil1_placebo1_unicefcol),
         mean(brazil2_normlegitimacy_stccol)-mean(brazil2_placebo1_stccol),
         mean(brazil2_performancelegitimacy_stccol)-mean(brazil2_placebo1_stccol),
         mean(brazil2_procedurallegitimacy_stccol)-mean(brazil2_placebo1_stccol))

mean1 = c(mean(brazil1_normlegitimacy_unicefcol),
          mean(brazil1_performancelegitimacy_unicefcol),
          mean(brazil1_procedurallegitimacy_unicefcol),
          mean(brazil2_normlegitimacy_stccol),
          mean(brazil2_performancelegitimacy_stccol),
          mean(brazil2_procedurallegitimacy_stccol))

mean2 = c(mean(brazil1_placebo1_unicefcol),
          mean(brazil1_placebo1_unicefcol),
          mean(brazil1_placebo1_unicefcol),
          mean(brazil2_placebo1_stccol),
          mean(brazil2_placebo1_stccol),
          mean(brazil2_placebo1_stccol))

se = c(std.error(brazil1_normlegitimacy_unicefcol),
       std.error(brazil1_performancelegitimacy_unicefcol),
       std.error(brazil1_procedurallegitimacy_unicefcol),
       std.error(brazil2_normlegitimacy_stccol),
       std.error(brazil2_performancelegitimacy_stccol),
       std.error(brazil2_procedurallegitimacy_stccol))

pvalue = c(t.test(brazil1_normlegitimacy_unicefcol,brazil1_placebo1_unicefcol)$p.value,
           t.test(brazil1_performancelegitimacy_unicefcol,brazil1_placebo1_unicefcol)$p.value,
           t.test(brazil1_procedurallegitimacy_unicefcol,brazil1_placebo1_unicefcol)$p.value,
           t.test(brazil2_normlegitimacy_stccol,brazil2_placebo1_stccol)$p.value,
           t.test(brazil2_performancelegitimacy_stccol,brazil2_placebo1_stccol)$p.value,
           t.test(brazil2_procedurallegitimacy_stccol,brazil2_placebo1_stccol)$p.value)

test1 <- t.test(brazil1_normlegitimacy_unicefcol,brazil1_placebo1_unicefcol)
test2 <- t.test(brazil1_performancelegitimacy_unicefcol,brazil1_placebo1_unicefcol)
test3 <- t.test(brazil1_procedurallegitimacy_unicefcol,brazil1_placebo1_unicefcol)
test4 <- t.test(brazil2_normlegitimacy_stccol,brazil2_placebo1_stccol)
test5 <- t.test(brazil2_performancelegitimacy_stccol,brazil2_placebo1_stccol)
test6 <- t.test(brazil2_procedurallegitimacy_stccol,brazil2_placebo1_stccol)

sample_size_test1 <- length(brazil1_normlegitimacy_unicefcol) + length(brazil1_placebo1_unicefcol)
sample_size_test2 <- length(brazil1_performancelegitimacy_unicefcol) + length(brazil1_placebo1_unicefcol)
sample_size_test3 <- length(brazil1_procedurallegitimacy_unicefcol) + length(brazil1_placebo1_unicefcol)
sample_size_test4 <- length(brazil2_normlegitimacy_stccol) + length(brazil2_placebo1_stccol)
sample_size_test5 <- length(brazil2_performancelegitimacy_stccol) + length(brazil2_placebo1_stccol)
sample_size_test6 <- length(brazil2_procedurallegitimacy_stccol) + length(brazil2_placebo1_stccol)

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3, 
                sample_size_test4, sample_size_test5, sample_size_test6)

LowerCI = c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1], 
            test4$conf.int[1], test5$conf.int[1], test6$conf.int[1])
UpperCI = c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2], 
            test4$conf.int[2], test5$conf.int[2], test6$conf.int[2])

df_brazil_ab_r1 <- data.frame(country = rep(c("Brazil"),6),
                              test = tests,
                              difference = diff,
                              lowerci = LowerCI,
                              upperci = UpperCI,
                              mean1 = mean1, 
                              mean2 = mean2,
                              p.value = pvalue,
                              se = se,
                              sample.size = sample_sizes)


# Egypt
egypt1$nonclick=egypt1$Reach-egypt1$Unique.link.clicks
egypt2$nonclick=egypt2$Reach-egypt2$Unique.link.clicks

## COMBINATION 1
egypt1_normlegitimacy_unicef=subset(egypt1, Ad.Set.Name=='Charity_Egypt_c1_norm_unicef')
sum(egypt1_normlegitimacy_unicef$Unique.link.clicks)
sum(egypt1_normlegitimacy_unicef$nonclick)
egypt1_normlegitimacy_unicefcol=rep(c(1, 0), times = c(sum(egypt1_normlegitimacy_unicef$Unique.link.clicks),
                                                       sum(egypt1_normlegitimacy_unicef$nonclick)))

egypt1_performancelegitimacy_unicef=subset(egypt1, Ad.Set.Name=="Charity_Egypt_c1_performance_unicef")
sum(egypt1_performancelegitimacy_unicef$Unique.link.clicks)
sum(egypt1_performancelegitimacy_unicef$nonclick)
egypt1_performancelegitimacy_unicefcol=rep(c(1, 0), times = c(sum(egypt1_performancelegitimacy_unicef$Unique.link.clicks),
                                                              sum(egypt1_performancelegitimacy_unicef$nonclick)))

egypt1_procedurallegitimacy_unicef=subset(egypt1, Ad.Set.Name=="Charity_Egypt_c1_procedural_unicef")
sum(egypt1_procedurallegitimacy_unicef$Unique.link.clicks)
sum(egypt1_procedurallegitimacy_unicef$nonclick)
egypt1_procedurallegitimacy_unicefcol=rep(c(1, 0), times = c(sum(egypt1_procedurallegitimacy_unicef$Unique.link.clicks),
                                                             sum(egypt1_procedurallegitimacy_unicef$nonclick)))

egypt1_placebo1_unicef=subset(egypt1, Ad.Set.Name=="Charity_Egypt_c1_placebo1")
sum(egypt1_placebo1_unicef$Unique.link.clicks)
sum(egypt1_placebo1_unicef$nonclick)
egypt1_placebo1_unicefcol=rep(c(1, 0), times = c(sum(egypt1_placebo1_unicef$Unique.link.clicks),
                                                 sum(egypt1_placebo1_unicef$nonclick)))


## COMBINATION 2
egypt2_placebo1=subset(egypt2, Ad.Set.Name=='Charity_Egypt_c2_placebo1')
sum(egypt2_placebo1$Unique.link.clicks)
sum(egypt2_placebo1$nonclick)
egypt2_placebo1col=rep(c(1, 0), times = c(sum(egypt2_placebo1$Unique.link.clicks),
                                          sum(egypt2_placebo1$nonclick)))

egypt2_normlegitimacy_stc=subset(egypt2, Ad.Set.Name=='Charity_Egypt_c2_norm_stc')
sum(egypt2_normlegitimacy_stc$Unique.link.clicks)
sum(egypt2_normlegitimacy_stc$nonclick)
egypt2_normlegitimacy_stccol=rep(c(1, 0), times = c(sum(egypt2_normlegitimacy_stc$Unique.link.clicks), 
                                                    sum(egypt2_normlegitimacy_stc$nonclick)))

egypt2_performancelegitimacy_stc=subset(egypt2, Ad.Set.Name=="Charity_Egypt_c2_performance_stc")
sum(egypt2_performancelegitimacy_stc$Unique.link.clicks)
sum(egypt2_performancelegitimacy_stc$nonclick)
egypt2_performancelegitimacy_stccol=rep(c(1, 0), times = c(sum(egypt2_performancelegitimacy_stc$Unique.link.clicks),
                                                           sum(egypt2_performancelegitimacy_stc$nonclick)))

egypt2_procedurallegitimacy_stc=subset(egypt2, Ad.Set.Name=="Charity_Egypt_c2_procedural_stc")
sum(egypt2_procedurallegitimacy_stc$Unique.link.clicks)
sum(egypt2_procedurallegitimacy_stc$nonclick)
egypt2_procedurallegitimacy_stccol=rep(c(1, 0), times = c(sum(egypt2_procedurallegitimacy_stc$Unique.link.clicks),
                                                          sum(egypt2_procedurallegitimacy_stc$nonclick)))


## Egypt: Create Output Table
tests = c("Norm (UNICEF)", "Performance (UNICEF)", "Procedural (UNICEF)",
          "Norm  (STC)", "Performance (STC)", "Procedural (STC)")

diff = c(mean(egypt1_normlegitimacy_unicefcol)-mean(egypt1_placebo1_unicefcol),
         mean(egypt1_performancelegitimacy_unicefcol)-mean(egypt1_placebo1_unicefcol),
         mean(egypt1_procedurallegitimacy_unicefcol)-mean(egypt1_placebo1_unicefcol),
         mean(egypt2_normlegitimacy_stccol)-mean(egypt2_placebo1col),
         mean(egypt2_performancelegitimacy_stccol)-mean(egypt2_placebo1col),
         mean(egypt2_procedurallegitimacy_stccol)-mean(egypt2_placebo1col))

mean1 = c(mean(egypt1_normlegitimacy_unicefcol),
          mean(egypt1_performancelegitimacy_unicefcol),
          mean(egypt1_procedurallegitimacy_unicefcol),
          mean(egypt2_normlegitimacy_stccol),
          mean(egypt2_performancelegitimacy_stccol),
          mean(egypt2_procedurallegitimacy_stccol))

mean2 = c(mean(egypt1_placebo1_unicefcol),
          mean(egypt1_placebo1_unicefcol),
          mean(egypt1_placebo1_unicefcol),
          mean(egypt2_placebo1col),
          mean(egypt2_placebo1col),
          mean(egypt2_placebo1col))

se = c(std.error(egypt1_normlegitimacy_unicefcol),
       std.error(egypt1_performancelegitimacy_unicefcol),
       std.error(egypt1_procedurallegitimacy_unicefcol),
       std.error(egypt2_normlegitimacy_stccol),
       std.error(egypt2_performancelegitimacy_stccol),
       std.error(egypt2_procedurallegitimacy_stccol))

pvalue = c(t.test(egypt1_normlegitimacy_unicefcol,egypt1_placebo1_unicefcol)$p.value,
           t.test(egypt1_performancelegitimacy_unicefcol,egypt1_placebo1_unicefcol)$p.value,
           t.test(egypt1_procedurallegitimacy_unicefcol,egypt1_placebo1_unicefcol)$p.value,
           t.test(egypt2_normlegitimacy_stccol,egypt2_placebo1col)$p.value,
           t.test(egypt2_performancelegitimacy_stccol,egypt2_placebo1col)$p.value,
           t.test(egypt2_procedurallegitimacy_stccol,egypt2_placebo1col)$p.value)

test1 <- t.test(egypt1_normlegitimacy_unicefcol,egypt1_placebo1_unicefcol)
test2 <- t.test(egypt1_performancelegitimacy_unicefcol,egypt1_placebo1_unicefcol)
test3 <- t.test(egypt1_procedurallegitimacy_unicefcol,egypt1_placebo1_unicefcol)
test4 <- t.test(egypt2_normlegitimacy_stccol,egypt2_placebo1col)
test5 <- t.test(egypt2_performancelegitimacy_stccol,egypt2_placebo1col)
test6 <- t.test(egypt2_procedurallegitimacy_stccol,egypt2_placebo1col)

sample_size_test1 <- length(egypt1_normlegitimacy_unicefcol) + length(egypt1_placebo1_unicefcol)
sample_size_test2 <- length(egypt1_performancelegitimacy_unicefcol) + length(egypt1_placebo1_unicefcol)
sample_size_test3 <- length(egypt1_procedurallegitimacy_unicefcol) + length(egypt1_placebo1_unicefcol)
sample_size_test4 <- length(egypt2_normlegitimacy_stccol) + length(egypt2_placebo1col)
sample_size_test5 <- length(egypt2_performancelegitimacy_stccol) + length(egypt2_placebo1col)
sample_size_test6 <- length(egypt2_procedurallegitimacy_stccol) + length(egypt2_placebo1col)

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3, 
                sample_size_test4, sample_size_test5, sample_size_test6)

LowerCI = c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1], 
            test4$conf.int[1], test5$conf.int[1], test6$conf.int[1])
UpperCI = c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2], 
            test4$conf.int[2], test5$conf.int[2], test6$conf.int[2])

df_egypt_ab_r1 <- data.frame(country = rep(c("Egypt"),6),
                             test = tests,
                             difference = diff,
                             lowerci = LowerCI,
                             upperci = UpperCI,
                             mean1 = mean1, 
                             mean2 = mean2,
                             p.value = pvalue,
                             se = se,
                             sample.size = sample_sizes)


# Saudi Arabia
saudi1$nonclick=saudi1$Reach-saudi1$Unique.link.clicks
saudi2$nonclick=saudi2$Reach-saudi2$Unique.link.clicks

## COMBINATION 1
saudi1_normlegitimacy_unicef=subset(saudi1, Ad.Set.Name=='Charity_SA_c1_norm_unicef')
sum(saudi1_normlegitimacy_unicef$Unique.link.clicks)
sum(saudi1_normlegitimacy_unicef$nonclick)
saudi1_normlegitimacy_unicefcol=rep(c(1, 0), times = c(sum(saudi1_normlegitimacy_unicef$Unique.link.clicks),
                                                       sum(saudi1_normlegitimacy_unicef$nonclick)))

saudi1_performancelegitimacy_unicef=subset(saudi1, Ad.Set.Name=="Charity_SA_c1_performance_unicef")
sum(saudi1_performancelegitimacy_unicef$Unique.link.clicks)
sum(saudi1_performancelegitimacy_unicef$nonclick)
saudi1_performancelegitimacy_unicefcol=rep(c(1, 0), times = c(sum(saudi1_performancelegitimacy_unicef$Unique.link.clicks),
                                                              sum(saudi1_performancelegitimacy_unicef$nonclick)))

saudi1_procedurallegitimacy_unicef=subset(saudi1, Ad.Set.Name=="Charity_SA_c1_procedural_unicef")
sum(saudi1_procedurallegitimacy_unicef$Unique.link.clicks)
sum(saudi1_procedurallegitimacy_unicef$nonclick)
saudi1_procedurallegitimacy_unicefcol=rep(c(1, 0), times = c(sum(saudi1_procedurallegitimacy_unicef$Unique.link.clicks),
                                                             sum(saudi1_procedurallegitimacy_unicef$nonclick)))

saudi1_placebo1_unicef=subset(saudi1, Ad.Set.Name=="Charity_SA_c1_placebo1")
sum(saudi1_placebo1_unicef$Unique.link.clicks)
sum(saudi1_placebo1_unicef$nonclick)
saudi1_placebo1_unicefcol=rep(c(1, 0), times = c(sum(saudi1_placebo1_unicef$Unique.link.clicks),
                                                 sum(saudi1_placebo1_unicef$nonclick)))

## COMBINATION 2
saudi2_normlegitimacy_stc=subset(saudi2, Ad.Set.Name=='Charity_SA_c2_norm_stc')
sum(saudi2_normlegitimacy_stc$Unique.link.clicks)
sum(saudi2_normlegitimacy_stc$nonclick)
saudi2_normlegitimacy_stccol=rep(c(1, 0), times = c(sum(saudi2_normlegitimacy_stc$Unique.link.clicks), 
                                                    sum(saudi2_normlegitimacy_stc$nonclick)))

saudi2_performancelegitimacy_stc=subset(saudi2, Ad.Set.Name=="Charity_SA_c2_performance_stc")
sum(saudi2_performancelegitimacy_stc$Unique.link.clicks)
sum(saudi2_performancelegitimacy_stc$nonclick)
saudi2_performancelegitimacy_stccol=rep(c(1, 0), times = c(sum(saudi2_performancelegitimacy_stc$Unique.link.clicks),
                                                           sum(saudi2_performancelegitimacy_stc$nonclick)))

saudi2_placebo1_stc=subset(saudi2, Ad.Set.Name=="Charity_SA_c2_placebo1")
sum(saudi2_placebo1_stc$Unique.link.clicks)
sum(saudi2_placebo1_stc$nonclick)
saudi2_placebo1_stccol=rep(c(1, 0), times = c(sum(saudi2_placebo1_stc$Unique.link.clicks),
                                              sum(saudi2_placebo1_stc$nonclick)))

saudi2_procedurallegitimacy_stc=subset(saudi2, Ad.Set.Name=="Charity_SA_c2_procedural_stc")
sum(saudi2_procedurallegitimacy_stc$Unique.link.clicks)
sum(saudi2_procedurallegitimacy_stc$nonclick)
saudi2_procedurallegitimacy_stccol=rep(c(1, 0), times = c(sum(saudi2_procedurallegitimacy_stc$Unique.link.clicks),
                                                          sum(saudi2_procedurallegitimacy_stc$nonclick)))


## Saudi Arabia: Create Output Table
tests = c("Norm (UNICEF)", "Performance (UNICEF)", "Procedural (UNICEF)",
          "Norm  (STC)", "Performance (STC)", "Procedural (STC)")

diff = c(mean(saudi1_normlegitimacy_unicefcol)-mean(saudi1_placebo1_unicefcol),
         mean(saudi1_performancelegitimacy_unicefcol)-mean(saudi1_placebo1_unicefcol),
         mean(saudi1_procedurallegitimacy_unicefcol)-mean(saudi1_placebo1_unicefcol),
         mean(saudi2_normlegitimacy_stccol)-mean(saudi2_placebo1_stccol),
         mean(saudi2_performancelegitimacy_stccol)-mean(saudi2_placebo1_stccol),
         mean(saudi2_procedurallegitimacy_stccol)-mean(saudi2_placebo1_stccol))

mean1 = c(mean(saudi1_normlegitimacy_unicefcol),
          mean(saudi1_performancelegitimacy_unicefcol),
          mean(saudi1_procedurallegitimacy_unicefcol),
          mean(saudi2_normlegitimacy_stccol),
          mean(saudi2_performancelegitimacy_stccol),
          mean(saudi2_procedurallegitimacy_stccol))

mean2 = c(mean(saudi1_placebo1_unicefcol),
          mean(saudi1_placebo1_unicefcol),
          mean(saudi1_placebo1_unicefcol),
          mean(saudi2_placebo1_stccol),
          mean(saudi2_placebo1_stccol),
          mean(saudi2_placebo1_stccol))

se = c(std.error(saudi1_normlegitimacy_unicefcol),
       std.error(saudi1_performancelegitimacy_unicefcol),
       std.error(saudi1_procedurallegitimacy_unicefcol),
       std.error(saudi2_normlegitimacy_stccol),
       std.error(saudi2_performancelegitimacy_stccol),
       std.error(saudi2_procedurallegitimacy_stccol))

pvalue = c(t.test(saudi1_normlegitimacy_unicefcol,saudi1_placebo1_unicefcol)$p.value,
           t.test(saudi1_performancelegitimacy_unicefcol,saudi1_placebo1_unicefcol)$p.value,
           t.test(saudi1_procedurallegitimacy_unicefcol,saudi1_placebo1_unicefcol)$p.value,
           t.test(saudi2_normlegitimacy_stccol,saudi2_placebo1_stccol)$p.value,
           t.test(saudi2_performancelegitimacy_stccol,saudi2_placebo1_stccol)$p.value,
           t.test(saudi2_procedurallegitimacy_stccol,saudi2_placebo1_stccol)$p.value)

test1 <- t.test(saudi1_normlegitimacy_unicefcol,saudi1_placebo1_unicefcol)
test2 <- t.test(saudi1_performancelegitimacy_unicefcol,saudi1_placebo1_unicefcol)
test3 <- t.test(saudi1_procedurallegitimacy_unicefcol,saudi1_placebo1_unicefcol)
test4 <- t.test(saudi2_normlegitimacy_stccol,saudi2_placebo1_stccol)
test5 <- t.test(saudi2_performancelegitimacy_stccol,saudi2_placebo1_stccol)
test6 <- t.test(saudi2_procedurallegitimacy_stccol,saudi2_placebo1_stccol)

sample_size_test1 <- length(saudi1_normlegitimacy_unicefcol) + length(saudi1_placebo1_unicefcol)
sample_size_test2 <- length(saudi1_performancelegitimacy_unicefcol) + length(saudi1_placebo1_unicefcol)
sample_size_test3 <- length(saudi1_procedurallegitimacy_unicefcol) + length(saudi1_placebo1_unicefcol)
sample_size_test4 <- length(saudi2_normlegitimacy_stccol) + length(saudi2_placebo1_stccol)
sample_size_test5 <- length(saudi2_performancelegitimacy_stccol) + length(saudi2_placebo1_stccol)
sample_size_test6 <- length(saudi2_procedurallegitimacy_stccol) + length(saudi2_placebo1_stccol)

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3, 
                sample_size_test4, sample_size_test5, sample_size_test6)

LowerCI = c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1], 
            test4$conf.int[1], test5$conf.int[1], test6$conf.int[1])
UpperCI = c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2], 
            test4$conf.int[2], test5$conf.int[2], test6$conf.int[2])

df_saudi_ab_r1 <- data.frame(country = rep(c("SaudiArabia"),6),
                          test = tests,
                          difference = diff,
                          lowerci = LowerCI,
                          upperci = UpperCI,
                          mean1 = mean1, 
                          mean2 = mean2,
                          p.value = pvalue,
                          se = se,
                          sample.size = sample_sizes)


# United Kingdom
uk1$nonclick=uk1$Reach-uk1$Unique.link.clicks
uk2$nonclick=uk2$Reach-uk2$Unique.link.clicks

## COMBINATION 1
uk1_normlegitimacy_unicef=subset(uk1, Ad.Set.Name=='Charity_UK_c1_normlegitimacy_unicef')
sum(uk1_normlegitimacy_unicef$Unique.link.clicks)
sum(uk1_normlegitimacy_unicef$nonclick)
uk1_normlegitimacy_unicefcol=rep(c(1, 0), times = c(sum(uk1_normlegitimacy_unicef$Unique.link.clicks),
                                                    sum(uk1_normlegitimacy_unicef$nonclick)))

uk1_performancelegitimacy_unicef=subset(uk1, Ad.Set.Name=="Charity_UK_c1_performancelegitimacy_unicef")
sum(uk1_performancelegitimacy_unicef$Unique.link.clicks)
sum(uk1_performancelegitimacy_unicef$nonclick)
uk1_performancelegitimacy_unicefcol=rep(c(1, 0), times = c(sum(uk1_performancelegitimacy_unicef$Unique.link.clicks),
                                                           sum(uk1_performancelegitimacy_unicef$nonclick)))

uk1_procedurallegitimacy_unicef=subset(uk1, Ad.Set.Name=="Charity_UK_c1_procedurallegitimacy_unicef")
sum(uk1_procedurallegitimacy_unicef$Unique.link.clicks)
sum(uk1_procedurallegitimacy_unicef$nonclick)
uk1_procedurallegitimacy_unicefcol=rep(c(1, 0), times = c(sum(uk1_procedurallegitimacy_unicef$Unique.link.clicks),
                                                          sum(uk1_procedurallegitimacy_unicef$nonclick)))

uk1_placebo1_unicef=subset(uk1, Ad.Set.Name=="Charity_UK_c1_placebo1")
sum(uk1_placebo1_unicef$Unique.link.clicks)
sum(uk1_placebo1_unicef$nonclick)
uk1_placebo1_unicefcol=rep(c(1, 0), times = c(sum(uk1_placebo1_unicef$Unique.link.clicks),
                                              sum(uk1_placebo1_unicef$nonclick)))


## COMBINATION 2
uk2_normlegitimacy_stc=subset(uk2, Ad.Set.Name=='Charity_UK_c2_normlegitimacy_stc')
sum(uk2_normlegitimacy_stc$Unique.link.clicks)
sum(uk2_normlegitimacy_stc$nonclick)
uk2_normlegitimacy_stccol=rep(c(1, 0), times = c(sum(uk2_normlegitimacy_stc$Unique.link.clicks), 
                                                 sum(uk2_normlegitimacy_stc$nonclick)))

uk2_performancelegitimacy_stc=subset(uk2, Ad.Set.Name=="Charity_UK_c2_performancelegitimacy_stc")
sum(uk2_performancelegitimacy_stc$Unique.link.clicks)
sum(uk2_performancelegitimacy_stc$nonclick)
uk2_performancelegitimacy_stccol=rep(c(1, 0), times = c(sum(uk2_performancelegitimacy_stc$Unique.link.clicks),
                                                        sum(uk2_performancelegitimacy_stc$nonclick)))

uk2_placebo1_stc=subset(uk2, Ad.Set.Name=="Charity_UK_c2_placebo1")
sum(uk2_placebo1_stc$Unique.link.clicks)
sum(uk2_placebo1_stc$nonclick)
uk2_placebo1_stccol=rep(c(1, 0), times = c(sum(uk2_placebo1_stc$Unique.link.clicks),
                                           sum(uk2_placebo1_stc$nonclick)))

uk2_procedurallegitimacy_stc=subset(uk2, Ad.Set.Name=="Charity_UK_c2_procedurallegitimacy_stc")
sum(uk2_procedurallegitimacy_stc$Unique.link.clicks)
sum(uk2_procedurallegitimacy_stc$nonclick)
uk2_procedurallegitimacy_stccol=rep(c(1, 0), times = c(sum(uk2_procedurallegitimacy_stc$Unique.link.clicks),
                                                       sum(uk2_procedurallegitimacy_stc$nonclick)))


## United Kingdom: Create Output Table
tests = c("Norm (UNICEF)", "Performance (UNICEF)", "Procedural (UNICEF)",
          "Norm  (STC)", "Performance (STC)", "Procedural (STC)")

diff = c(mean(uk1_normlegitimacy_unicefcol)-mean(uk1_placebo1_unicefcol),
         mean(uk1_performancelegitimacy_unicefcol)-mean(uk1_placebo1_unicefcol),
         mean(uk1_procedurallegitimacy_unicefcol)-mean(uk1_placebo1_unicefcol),
         mean(uk2_normlegitimacy_stccol)-mean(uk2_placebo1_stccol),
         mean(uk2_performancelegitimacy_stccol)-mean(uk2_placebo1_stccol),
         mean(uk2_procedurallegitimacy_stccol)-mean(uk2_placebo1_stccol))

mean1 = c(mean(uk1_normlegitimacy_unicefcol),
          mean(uk1_performancelegitimacy_unicefcol),
          mean(uk1_procedurallegitimacy_unicefcol),
          mean(uk2_normlegitimacy_stccol),
          mean(uk2_performancelegitimacy_stccol),
          mean(uk2_procedurallegitimacy_stccol))

mean2 = c(mean(uk1_placebo1_unicefcol),
          mean(uk1_placebo1_unicefcol),
          mean(uk1_placebo1_unicefcol),
          mean(uk2_placebo1_stccol),
          mean(uk2_placebo1_stccol),
          mean(uk2_placebo1_stccol))

se = c(std.error(uk1_normlegitimacy_unicefcol),
       std.error(uk1_performancelegitimacy_unicefcol),
       std.error(uk1_procedurallegitimacy_unicefcol),
       std.error(uk2_normlegitimacy_stccol),
       std.error(uk2_performancelegitimacy_stccol),
       std.error(uk2_procedurallegitimacy_stccol))

pvalue = c(t.test(uk1_normlegitimacy_unicefcol,uk1_placebo1_unicefcol)$p.value,
           t.test(uk1_performancelegitimacy_unicefcol,uk1_placebo1_unicefcol)$p.value,
           t.test(uk1_procedurallegitimacy_unicefcol,uk1_placebo1_unicefcol)$p.value,
           t.test(uk2_normlegitimacy_stccol,uk2_placebo1_stccol)$p.value,
           t.test(uk2_performancelegitimacy_stccol,uk2_placebo1_stccol)$p.value,
           t.test(uk2_procedurallegitimacy_stccol,uk2_placebo1_stccol)$p.value)

sample_size_test1 <- length(uk1_normlegitimacy_unicefcol) + length(uk1_placebo1_unicefcol)
sample_size_test2 <- length(uk1_performancelegitimacy_unicefcol) + length(uk1_placebo1_unicefcol)
sample_size_test3 <- length(uk1_procedurallegitimacy_unicefcol) + length(uk1_placebo1_unicefcol)
sample_size_test4 <- length(uk2_normlegitimacy_stccol) + length(uk2_placebo1_stccol)
sample_size_test5 <- length(uk2_performancelegitimacy_stccol) + length(uk2_placebo1_stccol)
sample_size_test6 <- length(uk2_procedurallegitimacy_stccol) + length(uk2_placebo1_stccol)

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3, 
                sample_size_test4, sample_size_test5, sample_size_test6)

test1 <- t.test(uk1_normlegitimacy_unicefcol,uk1_placebo1_unicefcol)
test2 <- t.test(uk1_performancelegitimacy_unicefcol,uk1_placebo1_unicefcol)
test3 <- t.test(uk1_procedurallegitimacy_unicefcol,uk1_placebo1_unicefcol)
test4 <- t.test(uk2_normlegitimacy_stccol,uk2_placebo1_stccol)
test5 <- t.test(uk2_performancelegitimacy_stccol,uk2_placebo1_stccol)
test6 <- t.test(uk2_procedurallegitimacy_stccol,uk2_placebo1_stccol)

LowerCI = c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1], 
            test4$conf.int[1], test5$conf.int[1], test6$conf.int[1])
UpperCI = c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2], 
            test4$conf.int[2], test5$conf.int[2], test6$conf.int[2])

df_uk_ab_r1 <- data.frame(country = rep(c("UK"),6),
                          test = tests,
                          difference = diff,
                          lowerci = LowerCI,
                          upperci = UpperCI,
                          mean1 = mean1, 
                          mean2 = mean2,
                          p.value = pvalue,
                          se = se,
                          sample.size = sample_sizes)


## Appendix Table B.1., Table B.2. 
charity_r1_combined <- rbind(df_brazil_ab_r1, 
                             df_egypt_ab_r1,
                             df_india_ab_r1,
                             df_saudi_ab_r1,
                             df_uk_ab_r1)

charity_r1_combined[] <- lapply(charity_r1_combined, function(x) {
  if (is.numeric(x)) round(x, 6) else x
})

write.xlsx(charity_r1_combined, "appendix_table_b1_b2.xlsx")



# (2) Experiment 2 

## Load data 
india = read.csv('aggregate_r2_CHN.csv')
egypt = read.csv('aggregate_r2_CHN.csv')

# India
india$nonclick=india$Reach -india$Unique.link.clicks

india_placebo1=subset(india, Ad.Set.Name=='Charity - India - R2 - placebo1_short')
sum(india_placebo1$Unique.link.clicks)
sum(india_placebo1$nonclick)
india_placebo1col=rep(c(1, 0), times = c(sum(india_placebo1$Unique.link.clicks),
                                         sum(india_placebo1$nonclick)))

india_performance_unicef_short=subset(india, Ad.Set.Name=="Charity - India - R2 - performance_unicef_short")
sum(india_performance_unicef_short$Unique.link.clicks)
sum(india_performance_unicef_short$nonclick)
india_performance_unicef_shortcol=rep(c(1, 0), times = c(sum(india_performance_unicef_short$Unique.link.clicks),
                                                         sum(india_performance_unicef_short$nonclick)))

india_norm_unicef_short=subset(india, Ad.Set.Name=="Charity - India - R2 - norm_unicef_short")
sum(india_norm_unicef_short$Unique.link.clicks)
sum(india_norm_unicef_short$nonclick)
india_norm_unicef_shortcol=rep(c(1, 0), times = c(sum(india_norm_unicef_short$Unique.link.clicks),
                                                  sum(india_norm_unicef_short$nonclick)))

india_procedural_unicef_short=subset(india, Ad.Set.Name=="Charity - India - R2 - procedural_unicef_short")
sum(india_procedural_unicef_short$Unique.link.clicks)
sum(india_procedural_unicef_short$nonclick)
india_procedural_unicef_shortcol=rep(c(1, 0), times = c(sum(india_procedural_unicef_short$Unique.link.clicks),
                                                        sum(india_procedural_unicef_short$nonclick)))


## India: Create Output Table
tests = c("Norm", "Performance", "Procedural")

diff = c(mean(india_norm_unicef_shortcol)-mean(india_placebo1col),
         mean(india_performance_unicef_shortcol)-mean(india_placebo1col),
         mean(india_procedural_unicef_shortcol)-mean(india_placebo1col))

mean1 = c(mean(india_norm_unicef_shortcol),
          mean(india_performance_unicef_shortcol),
          mean(india_procedural_unicef_shortcol))

mean2 = c(mean(india_placebo1col),
          mean(india_placebo1col),
          mean(india_placebo1col))

se = c(std.error(india_norm_unicef_shortcol),
       std.error(india_performance_unicef_shortcol),
       std.error(india_procedural_unicef_shortcol))

pvalue = c(t.test(india_norm_unicef_shortcol,india_placebo1col)$p.value,
           t.test(india_performance_unicef_shortcol,india_placebo1col)$p.value,
           t.test(india_procedural_unicef_shortcol,india_placebo1col)$p.value)

test1 <- t.test(india_norm_unicef_shortcol,india_placebo1col)
test2 <- t.test(india_performance_unicef_shortcol,india_placebo1col)
test3 <- t.test(india_procedural_unicef_shortcol,india_placebo1col)

sample_size_test1 <- length(india_norm_unicef_shortcol) + length(india_placebo1col)
sample_size_test2 <- length(india_performance_unicef_shortcol) + length(india_placebo1col)
sample_size_test3 <- length(india_procedural_unicef_shortcol) + length(india_placebo1col)

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3)

LowerCI = c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1])
UpperCI = c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2])


df_india_ab_r2 <- data.frame(country = rep(c("India"),3),
                             test = tests,
                             difference = diff,
                             lowerci = LowerCI,
                             upperci = UpperCI,
                             mean1 = mean1,
                             mean2 = mean2,
                             p.value = pvalue,
                             se = se,
                             sample.size = sample_sizes)


# Egypt
egypt$nonclick=egypt$Reach-egypt$Unique.link.clicks

egypt_placebo1=subset(egypt, Ad.Set.Name=='Charity - Egypt - R2 - placebo1_short')
sum(egypt_placebo1$Unique.link.clicks)
sum(egypt_placebo1$nonclick)
egypt_placebo1col=rep(c(1, 0), times = c(sum(egypt_placebo1$Unique.link.clicks),
                                         sum(egypt_placebo1$nonclick)))

egypt_performance_unicef_short=subset(egypt, Ad.Set.Name=="Charity - Egypt - R2 - performance_unicef_short")
sum(egypt_performance_unicef_short$Unique.link.clicks)
sum(egypt_performance_unicef_short$nonclick)
egypt_performance_unicef_shortcol=rep(c(1, 0), times = c(sum(egypt_performance_unicef_short$Unique.link.clicks),
                                                         sum(egypt_performance_unicef_short$nonclick)))

egypt_norm_unicef_short=subset(egypt, Ad.Set.Name=="Charity - Egypt - R2 - norm_unicef_short")
sum(egypt_norm_unicef_short$Unique.link.clicks)
sum(egypt_norm_unicef_short$nonclick)
egypt_norm_unicef_shortcol=rep(c(1, 0), times = c(sum(egypt_norm_unicef_short$Unique.link.clicks),
                                                  sum(egypt_norm_unicef_short$nonclick)))

egypt_procedural_unicef_short=subset(egypt, Ad.Set.Name=="Charity - Egypt - R2 - procedural_unicef_short")
sum(egypt_procedural_unicef_short$Unique.link.clicks)
sum(egypt_procedural_unicef_short$nonclick)
egypt_procedural_unicef_shortcol=rep(c(1, 0), times = c(sum(egypt_procedural_unicef_short$Unique.link.clicks),
                                                        sum(egypt_procedural_unicef_short$nonclick)))

Egypt_placebo1_performance_ttest <-t.test(egypt_placebo1col, egypt_performance_unicef_shortcol)
Egypt_placebo1_norm_ttest <-t.test(egypt_placebo1col, egypt_norm_unicef_shortcol)
Egypt_placebo1_procedural_ttest <-t.test(egypt_placebo1col, egypt_procedural_unicef_shortcol)


## Egypt: Create Output Table
tests = c("Norm", "Performance", "Procedural")

diff = c(mean(egypt_norm_unicef_shortcol)-mean(egypt_placebo1col),
         mean(egypt_performance_unicef_shortcol)-mean(egypt_placebo1col),
         mean(egypt_procedural_unicef_shortcol)-mean(egypt_placebo1col))

mean1 = c(mean(egypt_norm_unicef_shortcol),
          mean(egypt_performance_unicef_shortcol),
          mean(egypt_procedural_unicef_shortcol))

mean2 = c(mean(egypt_placebo1col),
          mean(egypt_placebo1col),
          mean(egypt_placebo1col))

pvalue = c(t.test(egypt_norm_unicef_shortcol,egypt_placebo1col)$p.value,
           t.test(egypt_performance_unicef_shortcol,egypt_placebo1col)$p.value,
           t.test(egypt_procedural_unicef_shortcol,egypt_placebo1col)$p.value)

se = c(std.error(egypt_norm_unicef_shortcol),
       std.error(egypt_performance_unicef_shortcol),
       std.error(egypt_procedural_unicef_shortcol))

test1 <- t.test(egypt_norm_unicef_shortcol,egypt_placebo1col)
test2 <- t.test(egypt_performance_unicef_shortcol,egypt_placebo1col)
test3 <- t.test(egypt_procedural_unicef_shortcol,egypt_placebo1col)

sample_size_test1 <- length(egypt_norm_unicef_shortcol) + length(egypt_placebo1col)
sample_size_test2 <- length(egypt_performance_unicef_shortcol) + length(egypt_placebo1col)
sample_size_test3 <- length(egypt_procedural_unicef_shortcol) + length(egypt_placebo1col)

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3)

LowerCI = c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1])
UpperCI = c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2])


df_Egypt_ab_r2 <- data.frame(country = rep(c("Egypt"),3),
                             test = tests,
                             difference = diff,
                             lowerci = LowerCI,
                             upperci = UpperCI,
                             mean1 = mean1,
                             mean2 = mean2,
                             p.value = pvalue,
                             se = se,
                             sample.size = sample_sizes)


## Appendix Table B.3.
charity_r2_combined <- rbind(
  df_Egypt_ab_r2,
  df_india_ab_r2)

charity_r2_combined[] <- lapply(charity_r2_combined, function(x) {
  if (is.numeric(x)) round(x, 6) else x
})

write.xlsx(charity_r2_combined, "appendix_table_b3.xlsx")



# (3) Experiment 3

## Load Data
Egypt = read.csv('Egypt_survey_CHN.csv')
India = read.csv('India_survey_CHN.csv')

## Std error function
std.error <- function(x, na.rm = FALSE) {
  if (na.rm) {
    x <- na.omit(x)
  }
  sd(x) / sqrt(length(x))
}


# Egypt

## Create subsets 
treatments <- c(7, 8, 9, 10, 11, 12, 13, 16)

for(i in treatments) {
  assign(paste0("Egypt_T", i), 
         subset(Egypt, treatment == paste0("T", i), select = win_donate_agg))
  assign(paste0("Egypt_T", i, "c"), 
         subset(Egypt, treatment == paste0("T", i), select = clicked2))
  assign(paste0("Egypt_T", i, "w"), 
         subset(Egypt, treatment == paste0("T", i), select = howmuch_donate_translated))
}


## Egypt: Create Output Table
tests = c("Norm (UNICEF-long) (Intention)", "Performance (UNICEF-long) (Intention)", "Procedural (UNICEF-long) (Intention)",
          "Norm (UNICEF-long) (Clicks)", "Performance (UNICEF-long) (Clicks)","Procedural (UNICEF-long) (Clicks)",
          "Norm (UNICEF-long) (Winnings)", "Performance (UNICEF-long) (Winnings)", "Procedural (UNICEF-long) (Winnings)", 
          
          "Norm (UNICEF-short)(Intention)", "Performance (UNICEF-short)(Intention)", "Procedural (UNICEF-short) (Intention)", 
          "Norm (UNICEF-short)(Clicks)", "Performance (UNICEF-short)(Clicks)", "Procedural (UNICEF-short) (Clicks)",
          "Norm (UNICEF-short)(Winnings)", "Performance (UNICEF-short)(Winnings)", "Procedural (UNICEF-short) (Winnings)")


diff = c(mean(Egypt_T9$win_donate_agg, na.rm = TRUE)-mean(Egypt_T13$win_donate_agg, na.rm = TRUE),
         mean(Egypt_T11$win_donate_agg, na.rm = TRUE)-mean(Egypt_T13$win_donate_agg, na.rm = TRUE),
         mean(Egypt_T7$win_donate_agg, na.rm = TRUE)-mean(Egypt_T13$win_donate_agg, na.rm = TRUE),
         
         mean(Egypt_T9c$clicked2, na.rm = TRUE)-mean(Egypt_T13c$clicked2, na.rm = TRUE),
         mean(Egypt_T11c$clicked2, na.rm = TRUE)-mean(Egypt_T13c$clicked2, na.rm = TRUE),
         mean(Egypt_T7c$clicked2, na.rm = TRUE)-mean(Egypt_T13c$clicked2, na.rm = TRUE),
         
         mean(Egypt_T9w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE),
         mean(Egypt_T11w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE),
         mean(Egypt_T7w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE),
         
         mean(Egypt_T10$win_donate_agg, na.rm = TRUE)-mean(Egypt_T16$win_donate_agg, na.rm = TRUE),
         mean(Egypt_T12$win_donate_agg, na.rm = TRUE)-mean(Egypt_T16$win_donate_agg, na.rm = TRUE),
         mean(Egypt_T8$win_donate_agg, na.rm = TRUE)-mean(Egypt_T16$win_donate_agg, na.rm = TRUE),
         
         mean(Egypt_T10c$clicked2, na.rm = TRUE)-mean(Egypt_T16c$clicked2, na.rm = TRUE),
         mean(Egypt_T12c$clicked2, na.rm = TRUE)-mean(Egypt_T16c$clicked2, na.rm = TRUE),
         mean(Egypt_T8c$clicked2, na.rm = TRUE)-mean(Egypt_T16c$clicked2, na.rm = TRUE),
         
         mean(Egypt_T10w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE),
         mean(Egypt_T12w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE),
         mean(Egypt_T8w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE))

mean1 = c(mean(Egypt_T9$win_donate_agg, na.rm = TRUE),
          mean(Egypt_T11$win_donate_agg, na.rm = TRUE),
          mean(Egypt_T7$win_donate_agg, na.rm = TRUE),
          
          mean(Egypt_T9c$clicked2, na.rm = TRUE),
          mean(Egypt_T11c$clicked2, na.rm = TRUE),
          mean(Egypt_T7c$clicked2, na.rm = TRUE),
          
          mean(Egypt_T9w$howmuch_donate_translated, na.rm = TRUE),
          mean(Egypt_T11w$howmuch_donate_translated, na.rm = TRUE),
          mean(Egypt_T7w$howmuch_donate_translated, na.rm = TRUE),
          
          mean(Egypt_T10$win_donate_agg, na.rm = TRUE),
          mean(Egypt_T12$win_donate_agg, na.rm = TRUE),
          mean(Egypt_T8$win_donate_agg, na.rm = TRUE),
          
          mean(Egypt_T10c$clicked2, na.rm = TRUE),
          mean(Egypt_T12c$clicked2, na.rm = TRUE),
          mean(Egypt_T8c$clicked2, na.rm = TRUE),
          
          mean(Egypt_T10w$howmuch_donate_translated, na.rm = TRUE),
          mean(Egypt_T12w$howmuch_donate_translated, na.rm = TRUE),
          mean(Egypt_T8w$howmuch_donate_translated, na.rm = TRUE))

mean2 = c(mean(Egypt_T13$win_donate_agg, na.rm = TRUE),
          mean(Egypt_T13$win_donate_agg, na.rm = TRUE),
          mean(Egypt_T13$win_donate_agg, na.rm = TRUE),
          
          mean(Egypt_T13c$clicked2, na.rm = TRUE),
          mean(Egypt_T13c$clicked2, na.rm = TRUE),
          mean(Egypt_T13c$clicked2, na.rm = TRUE),
          
          mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE),
          mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE),
          mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE),
          
          mean(Egypt_T16$win_donate_agg, na.rm = TRUE),
          mean(Egypt_T16$win_donate_agg, na.rm = TRUE),
          mean(Egypt_T16$win_donate_agg, na.rm = TRUE),
          
          mean(Egypt_T16c$clicked2, na.rm = TRUE),
          mean(Egypt_T16c$clicked2, na.rm = TRUE),
          mean(Egypt_T16c$clicked2, na.rm = TRUE),
          
          mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE),
          mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE),
          mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE))

se = c(std.error(Egypt_T9$win_donate_agg, na.rm = TRUE),
       std.error(Egypt_T11$win_donate_agg, na.rm = TRUE),
       std.error(Egypt_T7$win_donate_agg, na.rm = TRUE),
       
       std.error(Egypt_T9c$clicked2, na.rm = TRUE),
       std.error(Egypt_T11c$clicked2, na.rm = TRUE),
       std.error(Egypt_T7c$clicked2, na.rm = TRUE),
       
       std.error(Egypt_T9w$howmuch_donate_translated, na.rm = TRUE),
       std.error(Egypt_T11w$howmuch_donate_translated, na.rm = TRUE),
       std.error(Egypt_T7w$howmuch_donate_translated, na.rm = TRUE),
       
       std.error(Egypt_T10$win_donate_agg, na.rm = TRUE),
       std.error(Egypt_T12$win_donate_agg, na.rm = TRUE),
       std.error(Egypt_T8$win_donate_agg, na.rm = TRUE),
       
       std.error(Egypt_T10c$clicked2, na.rm = TRUE),
       std.error(Egypt_T12c$clicked2, na.rm = TRUE),
       std.error(Egypt_T8c$clicked2, na.rm = TRUE),
       
       std.error(Egypt_T10w$howmuch_donate_translated, na.rm = TRUE),
       std.error(Egypt_T12w$howmuch_donate_translated, na.rm = TRUE),
       std.error(Egypt_T8w$howmuch_donate_translated, na.rm = TRUE))

pvalue = c(t.test(Egypt_T9$win_donate_agg,Egypt_T13$win_donate_agg)$p.value,
           t.test(Egypt_T11$win_donate_agg,Egypt_T13$win_donate_agg)$p.value,
           t.test(Egypt_T7$win_donate_agg,Egypt_T13$win_donate_agg)$p.value,
           
           t.test(Egypt_T9c$clicked2,Egypt_T13c$clicked2)$p.value,
           t.test(Egypt_T11c$clicked2,Egypt_T13c$clicked2)$p.value,
           t.test(Egypt_T7c$clicked2,Egypt_T13c$clicked2)$p.value,
           
           t.test(Egypt_T9w$howmuch_donate_translated,Egypt_T13w$howmuch_donate_translated)$p.value,
           t.test(Egypt_T11w$howmuch_donate_translated,Egypt_T13w$howmuch_donate_translated)$p.value,
           t.test(Egypt_T7w$howmuch_donate_translated,Egypt_T13w$howmuch_donate_translated)$p.value,
           
           t.test(Egypt_T10$win_donate_agg,Egypt_T16$win_donate_agg)$p.value,
           t.test(Egypt_T12$win_donate_agg,Egypt_T16$win_donate_agg)$p.value,
           t.test(Egypt_T8$win_donate_agg,Egypt_T16$win_donate_agg)$p.value,
           
           t.test(Egypt_T10c$clicked2,Egypt_T16c$clicked2)$p.value,
           t.test(Egypt_T12c$clicked2,Egypt_T16c$clicked2)$p.value,
           t.test(Egypt_T8c$clicked2,Egypt_T16c$clicked2)$p.value,
           
           t.test(Egypt_T10w$howmuch_donate_translated,Egypt_T16w$howmuch_donate_translated)$p.value,
           t.test(Egypt_T12w$howmuch_donate_translated,Egypt_T16w$howmuch_donate_translated)$p.value,
           t.test(Egypt_T8w$howmuch_donate_translated,Egypt_T16w$howmuch_donate_translated)$p.value)

sample_size_test1 <- length(Egypt_T9$win_donate_agg[!is.na(Egypt_T9$win_donate_agg)]) + 
  length(Egypt_T13$win_donate_agg[!is.na(Egypt_T13$win_donate_agg)])
sample_size_test2 <- length(Egypt_T11$win_donate_agg[!is.na(Egypt_T11$win_donate_agg)]) + 
  length(Egypt_T13$win_donate_agg[!is.na(Egypt_T13$win_donate_agg)])
sample_size_test3 <- length(Egypt_T7$win_donate_agg[!is.na(Egypt_T7$win_donate_agg)]) + 
  length(Egypt_T13$win_donate_agg[!is.na(Egypt_T13$win_donate_agg)])

sample_size_test4 <- length(Egypt_T9c$clicked2[!is.na(Egypt_T9c$clicked2)]) + 
  length(Egypt_T13c$clicked2[!is.na(Egypt_T13c$clicked2)])
sample_size_test5 <- length(Egypt_T11c$clicked2[!is.na(Egypt_T11c$clicked2)]) + 
  length(Egypt_T13c$clicked2[!is.na(Egypt_T13c$clicked2)])
sample_size_test6 <- length(Egypt_T7c$clicked2[!is.na(Egypt_T7c$clicked2)]) + 
  length(Egypt_T13c$clicked2[!is.na(Egypt_T13c$clicked2)])

sample_size_test7 <- length(Egypt_T9w$howmuch_donate_translated[!is.na(Egypt_T9w$howmuch_donate_translated)]) + 
  length(Egypt_T13w$howmuch_donate_translated[!is.na(Egypt_T13w$howmuch_donate_translated)])
sample_size_test8 <- length(Egypt_T11w$howmuch_donate_translated[!is.na(Egypt_T11w$howmuch_donate_translated)]) + 
  length(Egypt_T13w$howmuch_donate_translated[!is.na(Egypt_T13w$howmuch_donate_translated)])
sample_size_test9 <- length(Egypt_T7w$howmuch_donate_translated[!is.na(Egypt_T7w$howmuch_donate_translated)]) + 
  length(Egypt_T13w$howmuch_donate_translated[!is.na(Egypt_T13w$howmuch_donate_translated)])

sample_size_test10 <- length(Egypt_T10$win_donate_agg[!is.na(Egypt_T10$win_donate_agg)]) + 
  length(Egypt_T16$win_donate_agg[!is.na(Egypt_T16$win_donate_agg)])
sample_size_test11 <- length(Egypt_T12$win_donate_agg[!is.na(Egypt_T12$win_donate_agg)]) + 
  length(Egypt_T16$win_donate_agg[!is.na(Egypt_T16$win_donate_agg)])
sample_size_test12 <- length(Egypt_T8$win_donate_agg[!is.na(Egypt_T8$win_donate_agg)]) + 
  length(Egypt_T16$win_donate_agg[!is.na(Egypt_T16$win_donate_agg)])

sample_size_test13 <- length(Egypt_T10c$clicked2[!is.na(Egypt_T10c$clicked2)]) + 
  length(Egypt_T16c$clicked2[!is.na(Egypt_T16c$clicked2)])
sample_size_test14 <- length(Egypt_T12c$clicked2[!is.na(Egypt_T12c$clicked2)]) + 
  length(Egypt_T16c$clicked2[!is.na(Egypt_T16c$clicked2)])
sample_size_test15 <- length(Egypt_T8c$clicked2[!is.na(Egypt_T8c$clicked2)]) + 
  length(Egypt_T16c$clicked2[!is.na(Egypt_T16c$clicked2)])

sample_size_test16 <- length(Egypt_T10w$howmuch_donate_translated[!is.na(Egypt_T10w$howmuch_donate_translated)]) + 
  length(Egypt_T16w$howmuch_donate_translated[!is.na(Egypt_T16w$howmuch_donate_translated)])
sample_size_test17 <- length(Egypt_T12w$howmuch_donate_translated[!is.na(Egypt_T12w$howmuch_donate_translated)]) + 
  length(Egypt_T16w$howmuch_donate_translated[!is.na(Egypt_T16w$howmuch_donate_translated)])
sample_size_test18 <- length(Egypt_T8w$howmuch_donate_translated[!is.na(Egypt_T8w$howmuch_donate_translated)]) + 
  length(Egypt_T16w$howmuch_donate_translated[!is.na(Egypt_T16w$howmuch_donate_translated)])

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3, 
                sample_size_test4, sample_size_test5, sample_size_test6,
                sample_size_test7, sample_size_test8, sample_size_test9, 
                sample_size_test10, sample_size_test11, sample_size_test12,
                sample_size_test13, sample_size_test14, sample_size_test15, 
                sample_size_test16, sample_size_test17, sample_size_test18)

test1 <- t.test(Egypt_T9$win_donate_agg,Egypt_T13$win_donate_agg)
test2 <- t.test(Egypt_T11$win_donate_agg,Egypt_T13$win_donate_agg)
test3 <- t.test(Egypt_T7$win_donate_agg,Egypt_T13$win_donate_agg)

test4 <- t.test(Egypt_T9c$clicked2,Egypt_T13c$clicked2)
test5 <- t.test(Egypt_T11c$clicked2,Egypt_T13c$clicked2)
test6 <- t.test(Egypt_T7c$clicked2,Egypt_T13c$clicked2)

test7 <- t.test(Egypt_T9w$howmuch_donate_translated,Egypt_T13w$howmuch_donate_translated)
test8 <- t.test(Egypt_T11w$howmuch_donate_translated,Egypt_T13w$howmuch_donate_translated)
test9 <- t.test(Egypt_T7w$howmuch_donate_translated,Egypt_T13w$howmuch_donate_translated)

test10 <- t.test(Egypt_T10$win_donate_agg,Egypt_T16$win_donate_agg)
test11 <- t.test(Egypt_T12$win_donate_agg,Egypt_T16$win_donate_agg)
test12 <- t.test(Egypt_T8$win_donate_agg,Egypt_T16$win_donate_agg)

test13 <- t.test(Egypt_T10c$clicked2,Egypt_T16c$clicked2)
test14 <- t.test(Egypt_T12c$clicked2,Egypt_T16c$clicked2)
test15 <- t.test(Egypt_T8c$clicked2,Egypt_T16c$clicked2)

test16 <- t.test(Egypt_T10w$howmuch_donate_translated,Egypt_T16w$howmuch_donate_translated)
test17 <- t.test(Egypt_T12w$howmuch_donate_translated,Egypt_T16w$howmuch_donate_translated)
test18 <- t.test(Egypt_T8w$howmuch_donate_translated,Egypt_T16w$howmuch_donate_translated)

LowerCI <- c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1], 
             test4$conf.int[1], test5$conf.int[1], test6$conf.int[1],
             test7$conf.int[1], test8$conf.int[1], test9$conf.int[1], 
             test10$conf.int[1], test11$conf.int[1], test12$conf.int[1],
             test13$conf.int[1], test14$conf.int[1], test15$conf.int[1], 
             test16$conf.int[1], test17$conf.int[1], test18$conf.int[1])

UpperCI <- c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2], 
             test4$conf.int[2], test5$conf.int[2], test6$conf.int[2],
             test7$conf.int[2], test8$conf.int[2], test9$conf.int[2], 
             test10$conf.int[2], test11$conf.int[2], test12$conf.int[2],
             test13$conf.int[2], test14$conf.int[2], test15$conf.int[2], 
             test16$conf.int[2], test17$conf.int[2], test18$conf.int[2])


df_egypt_survey <- data.frame(country = rep(c("Egypt"),18),
                              test = tests,
                              difference = diff,
                              lowerci = LowerCI,
                              upperci = UpperCI,
                              mean1 = mean1,
                              mean2 = mean2,
                              p.value = pvalue,
                              se = se,
                              sample.size = sample_sizes)

df_egypt_survey_rounded <- df_egypt_survey %>%
  mutate_if(is.numeric, ~ round(., 6))


## Appendix Table A.3.
tests <- c(7, 8, 9, 10, 11, 12, 13, 16)

data.frame(
  T = tests,
  win_donate_agg_1 = sapply(mget(paste0("Egypt_T", tests)), function(df) sum(df$win_donate_agg == 1, na.rm = TRUE)),
  clicked2_1       = sapply(mget(paste0("Egypt_T", tests, "c")), function(df) sum(df$clicked2 == 1, na.rm = TRUE)),
  howmuch_donate_translated_mean = sapply(mget(paste0("Egypt_T", tests, "w")), function(df) mean(df$howmuch_donate_translated, na.rm = TRUE))
)

data.frame(
  T = tests,
  win_donate_agg = sapply(mget(paste0("Egypt_T", tests)), function(df) sum(!is.na(df$win_donate_agg))),
  clicked2        = sapply(mget(paste0("Egypt_T", tests, "c")), function(df) sum(!is.na(df$clicked2))),
  howmuch_donate_translated = sapply(mget(paste0("Egypt_T", tests, "w")), function(df) sum(!is.na(df$howmuch_donate_translated)))
)



# India
India = read.csv('India_survey_CHN.csv')

## Create subsets
treatments <- c(7, 8, 9, 10, 11, 12, 13, 16)

for(i in treatments) {
  assign(paste0("India_T", i), 
         subset(India, treatment == paste0("T", i), select = win_donate_agg))
  assign(paste0("India_T", i, "c"), 
         subset(India, treatment == paste0("T", i), select = clicked2))
  assign(paste0("India_T", i, "w"), 
         subset(India, treatment == paste0("T", i), select = howmuch_donate_translated))
}


## India: Create Output Table
tests = c("Norm (UNICEF-long) (Intention)", "Performance (UNICEF-long) (Intention)", "Procedural (UNICEF-long) (Intention)",
          "Norm (UNICEF-long) (Clicks)", "Performance (UNICEF-long) (Clicks)","Procedural (UNICEF-long) (Clicks)",
          "Norm (UNICEF-long) (Winnings)", "Performance (UNICEF-long) (Winnings)", "Procedural (UNICEF-long) (Winnings)", 
          
          "Norm (UNICEF-short)(Intention)", "Performance (UNICEF-short)(Intention)", "Procedural (UNICEF-short) (Intention)", 
          "Norm (UNICEF-short)(Clicks)", "Performance (UNICEF-short)(Clicks)", "Procedural (UNICEF-short) (Clicks)",
          "Norm (UNICEF-short)(Winnings)", "Performance (UNICEF-short)(Winnings)", "Procedural (UNICEF-short) (Winnings)")

diff = c(mean(India_T9$win_donate_agg, na.rm = TRUE)-mean(India_T13$win_donate_agg, na.rm = TRUE),
         mean(India_T11$win_donate_agg, na.rm = TRUE)-mean(India_T13$win_donate_agg, na.rm = TRUE),
         mean(India_T7$win_donate_agg, na.rm = TRUE)-mean(India_T13$win_donate_agg, na.rm = TRUE),
         
         mean(India_T9c$clicked2, na.rm = TRUE)-mean(India_T13c$clicked2, na.rm = TRUE),
         mean(India_T11c$clicked2, na.rm = TRUE)-mean(India_T13c$clicked2, na.rm = TRUE),
         mean(India_T7c$clicked2, na.rm = TRUE)-mean(India_T13c$clicked2, na.rm = TRUE),
         
         mean(India_T9w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T13w$howmuch_donate_translated, na.rm = TRUE),
         mean(India_T11w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T13w$howmuch_donate_translated, na.rm = TRUE),
         mean(India_T7w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T13w$howmuch_donate_translated, na.rm = TRUE),
      
         mean(India_T10$win_donate_agg, na.rm = TRUE)-mean(India_T16$win_donate_agg, na.rm = TRUE),
         mean(India_T12$win_donate_agg, na.rm = TRUE)-mean(India_T16$win_donate_agg, na.rm = TRUE),
         mean(India_T8$win_donate_agg, na.rm = TRUE)-mean(India_T16$win_donate_agg, na.rm = TRUE),
         
         mean(India_T10c$clicked2, na.rm = TRUE)-mean(India_T16c$clicked2, na.rm = TRUE),
         mean(India_T12c$clicked2, na.rm = TRUE)-mean(India_T16c$clicked2, na.rm = TRUE),
         mean(India_T8c$clicked2, na.rm = TRUE)-mean(India_T16c$clicked2, na.rm = TRUE),
         
         mean(India_T10w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T16w$howmuch_donate_translated, na.rm = TRUE),
         mean(India_T12w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T16w$howmuch_donate_translated, na.rm = TRUE),
         mean(India_T8w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T16w$howmuch_donate_translated, na.rm = TRUE))

mean1 = c(mean(India_T9$win_donate_agg, na.rm = TRUE),
          mean(India_T11$win_donate_agg, na.rm = TRUE),
          mean(India_T7$win_donate_agg, na.rm = TRUE),
          
          mean(India_T9c$clicked2, na.rm = TRUE),
          mean(India_T11c$clicked2, na.rm = TRUE),
          mean(India_T7c$clicked2, na.rm = TRUE),
          
          mean(India_T9w$howmuch_donate_translated, na.rm = TRUE),
          mean(India_T11w$howmuch_donate_translated, na.rm = TRUE),
          mean(India_T7w$howmuch_donate_translated, na.rm = TRUE),
          
          mean(India_T10$win_donate_agg, na.rm = TRUE),
          mean(India_T12$win_donate_agg, na.rm = TRUE),
          mean(India_T8$win_donate_agg, na.rm = TRUE),
          
          mean(India_T10c$clicked2, na.rm = TRUE),
          mean(India_T12c$clicked2, na.rm = TRUE),
          mean(India_T8c$clicked2, na.rm = TRUE),
          
          mean(India_T10w$howmuch_donate_translated, na.rm = TRUE),
          mean(India_T12w$howmuch_donate_translated, na.rm = TRUE),
          mean(India_T8w$howmuch_donate_translated, na.rm = TRUE))

mean2 = c(mean(India_T13$win_donate_agg, na.rm = TRUE),
          mean(India_T13$win_donate_agg, na.rm = TRUE),
          mean(India_T13$win_donate_agg, na.rm = TRUE),
          
          mean(India_T13c$clicked2, na.rm = TRUE),
          mean(India_T13c$clicked2, na.rm = TRUE),
          mean(India_T13c$clicked2, na.rm = TRUE),
          
          mean(India_T13w$howmuch_donate_translated, na.rm = TRUE),
          mean(India_T13w$howmuch_donate_translated, na.rm = TRUE),
          mean(India_T13w$howmuch_donate_translated, na.rm = TRUE),
          
          mean(India_T16$win_donate_agg, na.rm = TRUE),
          mean(India_T16$win_donate_agg, na.rm = TRUE),
          mean(India_T16$win_donate_agg, na.rm = TRUE),
          
          mean(India_T16c$clicked2, na.rm = TRUE),
          mean(India_T16c$clicked2, na.rm = TRUE),
          mean(India_T16c$clicked2, na.rm = TRUE),
          
          mean(India_T16w$howmuch_donate_translated, na.rm = TRUE),
          mean(India_T16w$howmuch_donate_translated, na.rm = TRUE),
          mean(India_T16w$howmuch_donate_translated, na.rm = TRUE))

se = c(std.error(India_T9$win_donate_agg, na.rm = TRUE),
       std.error(India_T11$win_donate_agg, na.rm = TRUE),
       std.error(India_T7$win_donate_agg, na.rm = TRUE),
       
       std.error(India_T9c$clicked2, na.rm = TRUE),
       std.error(India_T11c$clicked2, na.rm = TRUE),
       std.error(India_T7c$clicked2, na.rm = TRUE),
       
       std.error(India_T9w$howmuch_donate_translated, na.rm = TRUE),
       std.error(India_T11w$howmuch_donate_translated, na.rm = TRUE),
       std.error(India_T7w$howmuch_donate_translated, na.rm = TRUE),
       
       std.error(India_T10$win_donate_agg, na.rm = TRUE),
       std.error(India_T12$win_donate_agg, na.rm = TRUE),
       std.error(India_T8$win_donate_agg, na.rm = TRUE),
       
       std.error(India_T10c$clicked2, na.rm = TRUE),
       std.error(India_T12c$clicked2, na.rm = TRUE),
       std.error(India_T8c$clicked2, na.rm = TRUE),
       
       std.error(India_T10w$howmuch_donate_translated, na.rm = TRUE),
       std.error(India_T12w$howmuch_donate_translated, na.rm = TRUE),
       std.error(India_T8w$howmuch_donate_translated, na.rm = TRUE))

pvalue = c(t.test(India_T9$win_donate_agg,India_T13$win_donate_agg)$p.value,
           t.test(India_T11$win_donate_agg,India_T13$win_donate_agg)$p.value,
           t.test(India_T7$win_donate_agg,India_T13$win_donate_agg)$p.value,
           
           t.test(India_T9c$clicked2,India_T13c$clicked2)$p.value,
           t.test(India_T11c$clicked2,India_T13c$clicked2)$p.value,
           t.test(India_T7c$clicked2,India_T13c$clicked2)$p.value,
           
           t.test(India_T9w$howmuch_donate_translated,India_T13w$howmuch_donate_translated)$p.value,
           t.test(India_T11w$howmuch_donate_translated,India_T13w$howmuch_donate_translated)$p.value,
           t.test(India_T7w$howmuch_donate_translated,India_T13w$howmuch_donate_translated)$p.value,
           
           t.test(India_T10$win_donate_agg,India_T16$win_donate_agg)$p.value,
           t.test(India_T12$win_donate_agg,India_T16$win_donate_agg)$p.value,
           t.test(India_T8$win_donate_agg,India_T16$win_donate_agg)$p.value,
           
           t.test(India_T10c$clicked2,India_T16c$clicked2)$p.value,
           t.test(India_T12c$clicked2,India_T16c$clicked2)$p.value,
           t.test(India_T8c$clicked2,India_T16c$clicked2)$p.value,
           
           t.test(India_T10w$howmuch_donate_translated,India_T16w$howmuch_donate_translated)$p.value,
           t.test(India_T12w$howmuch_donate_translated,India_T16w$howmuch_donate_translated)$p.value,
           t.test(India_T8w$howmuch_donate_translated,India_T16w$howmuch_donate_translated)$p.value)

sample_size_test1 <- length(India_T9$win_donate_agg[!is.na(India_T9$win_donate_agg)]) + 
  length(India_T13$win_donate_agg[!is.na(India_T13$win_donate_agg)])
sample_size_test2 <- length(India_T11$win_donate_agg[!is.na(India_T11$win_donate_agg)]) + 
  length(India_T13$win_donate_agg[!is.na(India_T13$win_donate_agg)])
sample_size_test3 <- length(India_T7$win_donate_agg[!is.na(India_T7$win_donate_agg)]) + 
  length(India_T13$win_donate_agg[!is.na(India_T13$win_donate_agg)])

sample_size_test4 <- length(India_T9c$clicked2[!is.na(India_T9c$clicked2)]) + 
  length(India_T13c$clicked2[!is.na(India_T13c$clicked2)])
sample_size_test5 <- length(India_T11c$clicked2[!is.na(India_T11c$clicked2)]) + 
  length(India_T13c$clicked2[!is.na(India_T13c$clicked2)])
sample_size_test6 <- length(India_T7c$clicked2[!is.na(India_T7c$clicked2)]) + 
  length(India_T13c$clicked2[!is.na(India_T13c$clicked2)])

sample_size_test7 <- length(India_T9w$howmuch_donate_translated[!is.na(India_T9w$howmuch_donate_translated)]) + 
  length(India_T13w$howmuch_donate_translated[!is.na(India_T13w$howmuch_donate_translated)])
sample_size_test8 <- length(India_T11w$howmuch_donate_translated[!is.na(India_T11w$howmuch_donate_translated)]) + 
  length(India_T13w$howmuch_donate_translated[!is.na(India_T13w$howmuch_donate_translated)])
sample_size_test9 <- length(India_T7w$howmuch_donate_translated[!is.na(India_T7w$howmuch_donate_translated)]) + 
  length(India_T13w$howmuch_donate_translated[!is.na(India_T13w$howmuch_donate_translated)])

sample_size_test10 <- length(India_T10$win_donate_agg[!is.na(India_T10$win_donate_agg)]) + 
  length(India_T16$win_donate_agg[!is.na(India_T16$win_donate_agg)])
sample_size_test11 <- length(India_T12$win_donate_agg[!is.na(India_T12$win_donate_agg)]) + 
  length(India_T16$win_donate_agg[!is.na(India_T16$win_donate_agg)])
sample_size_test12 <- length(India_T8$win_donate_agg[!is.na(India_T8$win_donate_agg)]) + 
  length(India_T16$win_donate_agg[!is.na(India_T16$win_donate_agg)])

sample_size_test13 <- length(India_T10c$clicked2[!is.na(India_T10c$clicked2)]) + 
  length(India_T16c$clicked2[!is.na(India_T16c$clicked2)])
sample_size_test14 <- length(India_T12c$clicked2[!is.na(India_T12c$clicked2)]) + 
  length(India_T16c$clicked2[!is.na(India_T16c$clicked2)])
sample_size_test15 <- length(India_T8c$clicked2[!is.na(India_T8c$clicked2)]) + 
  length(India_T16c$clicked2[!is.na(India_T16c$clicked2)])

sample_size_test16 <- length(India_T10w$howmuch_donate_translated[!is.na(India_T10w$howmuch_donate_translated)]) + 
  length(India_T16w$howmuch_donate_translated[!is.na(India_T16w$howmuch_donate_translated)])
sample_size_test17 <- length(India_T12w$howmuch_donate_translated[!is.na(India_T12w$howmuch_donate_translated)]) + 
  length(India_T16w$howmuch_donate_translated[!is.na(India_T16w$howmuch_donate_translated)])
sample_size_test18 <- length(India_T8w$howmuch_donate_translated[!is.na(India_T8w$howmuch_donate_translated)]) + 
  length(India_T16w$howmuch_donate_translated[!is.na(India_T16w$howmuch_donate_translated)])

sample_sizes= c(sample_size_test1, sample_size_test2, sample_size_test3, 
                sample_size_test4, sample_size_test5, sample_size_test6,
                sample_size_test7, sample_size_test8, sample_size_test9, 
                sample_size_test10, sample_size_test11, sample_size_test12,
                sample_size_test13, sample_size_test14, sample_size_test15, 
                sample_size_test16, sample_size_test17, sample_size_test18)

test1 <- t.test(India_T9$win_donate_agg,India_T13$win_donate_agg)
test2 <- t.test(India_T11$win_donate_agg,India_T13$win_donate_agg)
test3 <- t.test(India_T7$win_donate_agg,India_T13$win_donate_agg)

test4 <- t.test(India_T9c$clicked2,India_T13c$clicked2)
test5 <- t.test(India_T11c$clicked2,India_T13c$clicked2)
test6 <- t.test(India_T7c$clicked2,India_T13c$clicked2)

test7 <- t.test(India_T9w$howmuch_donate_translated,India_T13w$howmuch_donate_translated)
test8 <- t.test(India_T11w$howmuch_donate_translated,India_T13w$howmuch_donate_translated)
test9 <- t.test(India_T7w$howmuch_donate_translated,India_T13w$howmuch_donate_translated)

test10 <- t.test(India_T10$win_donate_agg,India_T16$win_donate_agg)
test11 <- t.test(India_T12$win_donate_agg,India_T16$win_donate_agg)
test12 <- t.test(India_T8$win_donate_agg,India_T16$win_donate_agg)

test13 <- t.test(India_T10c$clicked2,India_T16c$clicked2)
test14 <- t.test(India_T12c$clicked2,India_T16c$clicked2)
test15 <- t.test(India_T8c$clicked2,India_T16c$clicked2)

test16 <- t.test(India_T10w$howmuch_donate_translated,India_T16w$howmuch_donate_translated)
test17 <- t.test(India_T12w$howmuch_donate_translated,India_T16w$howmuch_donate_translated)
test18 <- t.test(India_T8w$howmuch_donate_translated,India_T16w$howmuch_donate_translated)

LowerCI <- c(test1$conf.int[1], test2$conf.int[1], test3$conf.int[1], 
             test4$conf.int[1], test5$conf.int[1], test6$conf.int[1],
             test7$conf.int[1], test8$conf.int[1], test9$conf.int[1], 
             test10$conf.int[1], test11$conf.int[1], test12$conf.int[1],
             test13$conf.int[1], test14$conf.int[1], test15$conf.int[1], 
             test16$conf.int[1], test17$conf.int[1], test18$conf.int[1])

UpperCI <- c(test1$conf.int[2], test2$conf.int[2], test3$conf.int[2], 
             test4$conf.int[2], test5$conf.int[2], test6$conf.int[2],
             test7$conf.int[2], test8$conf.int[2], test9$conf.int[2], 
             test10$conf.int[2], test11$conf.int[2], test12$conf.int[2],
             test13$conf.int[2], test14$conf.int[2], test15$conf.int[2], 
             test16$conf.int[2], test17$conf.int[2], test18$conf.int[2])

df_india_survey <- data.frame(country = rep(c("India"),18),
                              test = tests,
                              difference = diff,
                              p.value = pvalue,
                              lowerci = LowerCI,
                              upperci = UpperCI,
                              mean1 = mean1,
                              mean2 = mean2,
                              se = se,
                              sample.size = sample_sizes)

df_india_survey_rounded <- df_india_survey %>%
  mutate_if(is.numeric, ~ round(., 6))


## Appendix Table B.4., Table B.5., Table B.6.
charity_survey_combined <- rbind(df_egypt_survey_rounded, 
                                 df_india_survey_rounded)

write.xlsx(charity_survey_combined, "appendix_table_b4_b5_b6.xlsx")




## Appendix Table A.3.
tests <- c(7, 8, 9, 10, 11, 12, 13, 16)

data.frame(
  T = tests,
  win_donate_agg_1 = sapply(mget(paste0("India_T", tests)), function(df) sum(df$win_donate_agg == 1, na.rm = TRUE)),
  clicked2_1       = sapply(mget(paste0("India_T", tests, "c")), function(df) sum(df$clicked2 == 1, na.rm = TRUE)),
  howmuch_donate_translated_mean = sapply(mget(paste0("India_T", tests, "w")), function(df) mean(df$howmuch_donate_translated, na.rm = TRUE))
)

data.frame(
  T = tests,
  win_donate_agg = sapply(mget(paste0("India_T", tests)), function(df) sum(!is.na(df$win_donate_agg))),
  clicked2        = sapply(mget(paste0("India_T", tests, "c")), function(df) sum(!is.na(df$clicked2))),
  howmuch_donate_translated = sapply(mget(paste0("India_T", tests, "w")), function(df) sum(!is.na(df$howmuch_donate_translated))))




## Appendix Table B.7. (Benjamini-Hochberg multiple comparison adjustment)
charity_r1_combined$round <- "R1"
charity_r2_combined$round <- "R2"
charity_survey_combined$round <- "survey"

charity_combined <- rbind(charity_r1_combined, charity_r2_combined, charity_survey_combined)

charity_combined$p.value.adj.bh <- p.adjust(charity_combined$p.value, method = "BH")

n_tests <- nrow(charity_combined)
fdr_level <- 0.10
sorted_pvals <- sort(charity_combined$p.value)

bh_threshold_idx <- max(which(sorted_pvals <= (1:n_tests / n_tests) * fdr_level))
bh_critical_pvalue <- sorted_pvals[bh_threshold_idx]

cat("BH critical p-value threshold for 10% FDR:", bh_critical_pvalue, "\n")

## Appendix Table B.7. (Create Output Table)
bh_results <- data.frame(
  Country = charity_combined$country,
  Comparison = paste(charity_combined$test, "vs. Placebo"),
  Estimate = charity_combined$difference,
  `P-value` = charity_combined$p.value,
  Rank = rank(charity_combined$p.value, ties.method = "first"),
  `BH value (10% FDR)` = charity_combined$p.value.adj.bh,
  check.names = FALSE)

bh_results <- bh_results[order(bh_results$`P-value`), ]
bh_results$Rank <- 1:nrow(bh_results)

bh_results$Estimate <- sprintf("%.6f", bh_results$Estimate)

bh_results$Estimate <- ifelse(bh_results$`BH value (10% FDR)` < 0.10,
                              paste0(bh_results$Estimate, "*"),
                              bh_results$Estimate)

bh_results$`P-value` <- round(bh_results$`P-value`, 6)
bh_results$`BH value (10% FDR)` <- round(bh_results$`BH value (10% FDR)`, 8)

write.csv(bh_results, "appendix_table_b7.csv", row.names = FALSE)



# Table 2

## Load packages
library(metafor)
library(meta)

## Performance legitimacy
tests = c(rep("performance", 24)) 

experiment.type <- c("abtest_r1", "abtest_r1",
                     "abtest_r1", "abtest_r1",
                     "abtest_r1", "abtest_r1", 
                     "abtest_r1", "abtest_r1", "abtest_r2", "survey", "survey", "survey", "survey","survey", "survey",
                     "abtest_r1", "abtest_r1", "abtest_r2", "survey", "survey", "survey", "survey","survey", "survey")

treatment.wording <- c("long", "long",
                       "long", "long",
                       "long", "long",
                       "long", "long", "short", "long", "long", "long", "short", "short","short",
                       "long", "long", "short", "long", "long", "long", "short", "short","short")

outcome <- c("clicks", "clicks", 
             "clicks", "clicks", 
             "clicks", "clicks", 
             "clicks", "clicks", "clicks", "clicks", "intention", "winnings", "clicks", "intention", "winnings",
             "clicks", "clicks", "clicks", "clicks", "intention", "winnings", "clicks", "intention", "winnings")

organization <- c("IGO", "NGO",
                  "IGO", "NGO",
                  "IGO", "NGO",
                  "IGO", "NGO", "IGO", "IGO", "IGO", "IGO",  "IGO", "IGO", "IGO",
                  "IGO", "NGO", "IGO", "IGO", "IGO", "IGO",  "IGO", "IGO", "IGO")

year <- c("2022", "2022",
          "2022", "2022",
          "2022", "2022",
          "2022", "2022", "2023", "2023", "2023", "2023", "2023", "2023", "2023",
          "2022", "2022", "2023", "2023", "2023", "2023", "2023", "2023", "2023")

samplesize <- c(length(brazil1_performancelegitimacy_unicefcol)+length(brazil1_placebo1_unicefcol), ## experiment1
                length(brazil2_performancelegitimacy_stccol)+length(brazil2_placebo1_stccol), ## experiment1
                
                length(saudi1_performancelegitimacy_unicefcol)+length(saudi1_placebo1_unicefcol), ## experiment1
                length(saudi2_performancelegitimacy_stccol)+length(saudi2_placebo1_stccol), ## experiment1
                
                length(uk1_performancelegitimacy_unicefcol)+length(uk1_placebo1_unicefcol), ## experiment1
                length(uk2_performancelegitimacy_stccol)+length(uk2_placebo1_stccol), ## experiment1
                
                length(egypt1_performancelegitimacy_unicefcol)+length(egypt1_placebo1_unicefcol), ## experiment1
                length(egypt2_performancelegitimacy_stccol)+length(egypt2_placebo1col), ## experiment1
                length(egypt_performance_unicef_shortcol)+length(egypt_placebo1col), ## experiment2
                
                length(Egypt_T11c$clicked[!is.na(Egypt_T11c$clicked)])+length(Egypt_T13c$clicked[!is.na(Egypt_T13c$clicked)]), ## experiment3 (clicks) long unicef
                length(Egypt_T11$win_donate_agg[!is.na(Egypt_T11$win_donate_agg)])+length(Egypt_T13$win_donate_agg[!is.na(Egypt_T13$win_donate_agg)]), ## experiment3 (intention) long unicef
                length(Egypt_T11w$howmuch_donate_translated[!is.na(Egypt_T11w$howmuch_donate_translated)])+length(Egypt_T13w$howmuch_donate_translated[!is.na(Egypt_T13w$howmuch_donate_translated)]), ## experiment3 (winnings) long unicef
                length(Egypt_T12$win_donate_agg[!is.na(Egypt_T12$win_donate_agg)])+length(Egypt_T16$win_donate_agg[!is.na(Egypt_T16$win_donate_agg)]), ## experiment3 (clicks) short unicef
                length(Egypt_T12c$clicked[!is.na(Egypt_T12c$clicked)])+length(Egypt_T16c$clicked[!is.na(Egypt_T16c$clicked)]), ## experiment3 (intention) short unicef
                length(Egypt_T12w$howmuch_donate_translated[!is.na(Egypt_T12w$howmuch_donate_translated)])+length(Egypt_T16w$howmuch_donate_translated[!is.na(Egypt_T16w$howmuch_donate_translated)]), ## experiment3 (winnings) short unicef
                
                length(india1_performancelegitimacy_unicefcol)+length(india1_placebo1_unicefcol), ## experiment1
                length(india2_performancelegitimacy_stccol)+length(india2_placebo1col), ## experiment1
                length(india_performance_unicef_shortcol)+length(india_placebo1col), ## experiment2
                
                length(India_T11$win_donate_agg[!is.na(India_T11$win_donate_agg)])+length(India_T13$win_donate_agg[!is.na(India_T13$win_donate_agg)]), ## experiment3 (clicks) long unicef
                length(India_T11c$clicked[!is.na(India_T11c$clicked)])+length(India_T13c$clicked[!is.na(India_T13c$clicked)]), ## experiment3 (intention) long unicef
                length(India_T11w$howmuch_donate_translated[!is.na(India_T11w$howmuch_donate_translated)])+length(India_T13w$howmuch_donate_translated[!is.na(India_T13w$howmuch_donate_translated)]), ## experiment3 (winnings) long unicef
                length(India_T12$win_donate_agg[!is.na(India_T12$win_donate_agg)])+length(India_T16$win_donate_agg[!is.na(India_T16$win_donate_agg)]), ## experiment3 (clicks) short unicef
                length(India_T12c$clicked[!is.na(India_T12c$clicked)])+length(India_T16c$clicked[!is.na(India_T16c$clicked)]), ## experiment3 (intention) short unicef
                length(India_T12w$howmuch_donate_translated[!is.na(India_T12w$howmuch_donate_translated)])+length(India_T16w$howmuch_donate_translated[!is.na(India_T16w$howmuch_donate_translated)]) ## experiment3 (winnings) short unicef
) 


countries <- c(rep("Brazil", 2), rep("Saudi Arabia", 2), rep("United Kingdom", 2), rep("Egypt", 9), rep("India", 9))

TE <- c(mean(brazil1_performancelegitimacy_unicefcol)-mean(brazil1_placebo1_unicefcol), ## experiment1
        mean(brazil2_performancelegitimacy_stccol)-mean(brazil2_placebo1_stccol), ## experiment1
        
        mean(saudi1_performancelegitimacy_unicefcol)-mean(saudi1_placebo1_unicefcol), ## experiment1
        mean(saudi2_performancelegitimacy_stccol)-mean(saudi2_placebo1_stccol), ## experiment1
        
        mean(uk1_performancelegitimacy_unicefcol)-mean(uk1_placebo1_unicefcol), ## experiment1
        mean(uk2_performancelegitimacy_stccol)-mean(uk2_placebo1_stccol), ## experiment1
        
        mean(egypt1_performancelegitimacy_unicefcol)-mean(egypt1_placebo1_unicefcol), ## experiment1
        mean(egypt2_performancelegitimacy_stccol)-mean(egypt2_placebo1col), ## experiment1
        
        mean(egypt_performance_unicef_shortcol)-mean(egypt_placebo1col), ## experiment2
        
        mean(Egypt_T11c$clicked, na.rm = TRUE)-mean(Egypt_T13c$clicked, na.rm = TRUE), ## experiment3 (clicks) long unicef
        mean(Egypt_T11$win_donate_agg, na.rm = TRUE)-mean(Egypt_T13$win_donate_agg, na.rm = TRUE), ## experiment3 (intention) long unicef
        mean(Egypt_T11w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) long unicef
        mean(Egypt_T12$win_donate_agg, na.rm = TRUE)-mean(Egypt_T16$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) short unicef
        mean(Egypt_T12c$clicked, na.rm = TRUE)-mean(Egypt_T16c$clicked, na.rm = TRUE), ## experiment3 (intention) short unicef
        mean(Egypt_T12w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) short unicef
        
        mean(india1_performancelegitimacy_unicefcol)-mean(india1_placebo1_unicefcol), ## experiment1
        mean(india2_performancelegitimacy_stccol)-mean(india2_placebo1col), ## experiment1
        
        mean(india_performance_unicef_shortcol)-mean(india_placebo1col), ## experiment2
        
        mean(India_T11$win_donate_agg, na.rm = TRUE)-mean(India_T13$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) long unicef
        mean(India_T11c$clicked, na.rm = TRUE)-mean(India_T13c$clicked, na.rm = TRUE), ## experiment3 (intention) long unicef
        mean(India_T11w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T13w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) long unicef
        mean(India_T12$win_donate_agg, na.rm = TRUE)-mean(India_T16$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) short unicef
        mean(India_T12c$clicked, na.rm = TRUE)-mean(India_T16c$clicked, na.rm = TRUE), ## experiment3 (intention) short unicef
        mean(India_T12w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef
) 


brazil_abtest_r1_performance_unicef_tt <- t.test(brazil1_performancelegitimacy_unicefcol, brazil1_placebo1_unicefcol) ## experiment1
brazil_abtest_r1_performance_stc_tt <- t.test(brazil2_performancelegitimacy_stccol, brazil2_placebo1_stccol) ## experiment1

sa_abtest_r1_performance_unicef_tt <- t.test(saudi1_performancelegitimacy_unicefcol, saudi1_placebo1_unicefcol) ## experiment1
sa_abtest_r1_performance_stc_tt <- t.test(saudi2_performancelegitimacy_stccol, saudi2_placebo1_stccol) ## experiment1

uk_abtest_r1_performance_unicef_tt <- t.test(uk1_performancelegitimacy_unicefcol, uk1_placebo1_unicefcol) ## experiment1
uk_abtest_r1_performance_stc_tt <- t.test(uk2_performancelegitimacy_stccol, uk2_placebo1_stccol) ## experiment1

egypt_abtest_r1_performance_unicef_tt <- t.test(egypt1_performancelegitimacy_unicefcol, egypt1_placebo1_unicefcol) ## experiment1
egypt_abtest_r1_performance_stc_tt <- t.test(egypt2_performancelegitimacy_stccol, egypt2_placebo1col) ## experiment1
egypt_abtest_r2_performance_uniceflong_tt <- t.test(egypt_performance_unicef_shortcol, egypt_placebo1col) ## experiment2

egypt_survey_performance_uniceflong_click_tt <- t.test(Egypt_T11c$clicked, Egypt_T13c$clicked, na.rm = TRUE) ## experiment3 (clicks) long unicef
egypt_survey_performance_uniceflong_intention_tt <- t.test(Egypt_T11$win_donate_agg, Egypt_T13$win_donate_agg, na.rm = TRUE) ## experiment3 (intention) long unicef
egypt_survey_performance_uniceflong_winning_tt <- t.test(Egypt_T11w$howmuch_donate_translated, Egypt_T13w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) long unicef
egypt_survey_performance_unicefshort_click_tt <- t.test(Egypt_T12$win_donate_agg, Egypt_T16$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) short unicef
egypt_survey_performance_unicefshort_intention_tt <- t.test(Egypt_T12c$clicked, Egypt_T16c$clicked, na.rm = TRUE) ## experiment3 (intention) short unicef
egypt_survey_performance_unicefshort_winning_tt <- t.test(Egypt_T12w$howmuch_donate_translated, Egypt_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef

india_abtest_r1_performance_unicef_tt <- t.test(india1_performancelegitimacy_unicefcol, india1_placebo1_unicefcol) ## experiment1
india_abtest_r1_performance_stc_tt <- t.test(india2_performancelegitimacy_stccol, india2_placebo1col) ## experiment1
india_abtest_r2_performance_uniceflong_tt <- t.test(india_performance_unicef_shortcol, india_placebo1col) ## experiment2

india_survey_performance_uniceflong_click_tt <- t.test(India_T11$win_donate_agg, India_T13$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) long unicef
india_survey_performance_uniceflong_intention_tt <-t.test(India_T11c$clicked, India_T13c$clicked, na.rm = TRUE) ## experiment3 (intention) long unicef
india_survey_performance_uniceflong_winning_tt <- t.test(India_T11w$howmuch_donate_translated, India_T13w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) long unicef
india_survey_performance_unicefshort_click_tt <- t.test(India_T12$win_donate_agg, India_T16$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) short unicef
india_survey_performance_unicefshort_intention_tt <- t.test(India_T12c$clicked, India_T16c$clicked, na.rm = TRUE) ## experiment3 (intention) short unicef
india_survey_performance_unicefshort_winning_tt <- t.test(India_T12w$howmuch_donate_translated, India_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef

brazil_abtest_r1_performance_unicef_tt_se <- sqrt(brazil_abtest_r1_performance_unicef_tt$stderr)
brazil_abtest_r1_performance_stc_tt_se <- sqrt(brazil_abtest_r1_performance_stc_tt$stderr)

sa_abtest_r1_performance_unicef_tt_se <- sqrt(sa_abtest_r1_performance_unicef_tt$stderr)
sa_abtest_r1_performance_stc_tt_se <- sqrt(sa_abtest_r1_performance_stc_tt$stderr)

uk_abtest_r1_performance_unicef_tt_se <- sqrt(uk_abtest_r1_performance_unicef_tt$stderr)
uk_abtest_r1_performance_stc_tt_se <- sqrt(uk_abtest_r1_performance_stc_tt$stderr)

egypt_abtest_r1_performance_unicef_tt_se <- sqrt(egypt_abtest_r1_performance_unicef_tt$stderr)
egypt_abtest_r1_performance_stc_tt_se <- sqrt(egypt_abtest_r1_performance_stc_tt$stderr)
egypt_abtest_r2_performance_uniceflong_tt_se <- sqrt(egypt_abtest_r2_performance_uniceflong_tt$stderr)
egypt_survey_performance_uniceflong_click_tt_se <- sqrt(egypt_survey_performance_uniceflong_click_tt$stderr)
egypt_survey_performance_uniceflong_intention_tt_se <- sqrt(egypt_survey_performance_uniceflong_intention_tt$stderr)
egypt_survey_performance_uniceflong_winning_tt_se <- sqrt(egypt_survey_performance_uniceflong_winning_tt$stderr)
egypt_survey_performance_unicefshort_click_tt_se <- sqrt(egypt_survey_performance_unicefshort_click_tt$stderr)
egypt_survey_performance_unicefshort_intention_tt_se <- sqrt(egypt_survey_performance_unicefshort_intention_tt$stderr)
egypt_survey_performance_unicefshort_winning_tt_se <- sqrt(egypt_survey_performance_unicefshort_winning_tt$stderr)

india_abtest_r1_performance_unicef_tt_se <- sqrt(india_abtest_r1_performance_unicef_tt$stderr)
india_abtest_r1_performance_stc_tt_se <- sqrt(india_abtest_r1_performance_stc_tt$stderr)
india_abtest_r2_performance_uniceflong_tt_se <- sqrt(india_abtest_r2_performance_uniceflong_tt$stderr)
india_survey_performance_uniceflong_click_tt_se <- sqrt(india_survey_performance_uniceflong_click_tt$stderr)
india_survey_performance_uniceflong_intention_tt_se <- sqrt(india_survey_performance_uniceflong_intention_tt$stderr)
india_survey_performance_uniceflong_winning_tt_se <- sqrt(india_survey_performance_uniceflong_winning_tt$stderr)
india_survey_performance_unicefshort_click_tt_se <- sqrt(india_survey_performance_unicefshort_click_tt$stderr)
india_survey_performance_unicefshort_intention_tt_se <- sqrt(india_survey_performance_unicefshort_intention_tt$stderr)
india_survey_performance_unicefshort_winning_tt_se <- sqrt(india_survey_performance_unicefshort_winning_tt$stderr)

seTE <- c(brazil_abtest_r1_performance_unicef_tt_se, brazil_abtest_r1_performance_stc_tt_se,
          sa_abtest_r1_performance_unicef_tt_se, sa_abtest_r1_performance_stc_tt_se,
          uk_abtest_r1_performance_unicef_tt_se, uk_abtest_r1_performance_stc_tt_se,
          egypt_abtest_r1_performance_unicef_tt_se, egypt_abtest_r1_performance_stc_tt_se,
          egypt_abtest_r2_performance_uniceflong_tt_se,
          egypt_survey_performance_uniceflong_click_tt_se, egypt_survey_performance_uniceflong_intention_tt_se, egypt_survey_performance_uniceflong_winning_tt_se,
          egypt_survey_performance_unicefshort_click_tt_se, egypt_survey_performance_unicefshort_intention_tt_se, egypt_survey_performance_unicefshort_winning_tt_se,
          
          india_abtest_r1_performance_unicef_tt_se, india_abtest_r1_performance_stc_tt_se,
          india_abtest_r2_performance_uniceflong_tt_se,
          india_survey_performance_uniceflong_click_tt_se, india_survey_performance_uniceflong_intention_tt_se, india_survey_performance_uniceflong_winning_tt_se,
          india_survey_performance_unicefshort_click_tt_se, india_survey_performance_unicefshort_intention_tt_se, india_survey_performance_unicefshort_winning_tt_se)

pvalue <- c(t.test(brazil1_performancelegitimacy_unicefcol, brazil1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(brazil2_performancelegitimacy_stccol, brazil2_placebo1_stccol)$p.value, ## experiment1
            t.test(saudi1_performancelegitimacy_unicefcol, saudi1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(saudi2_performancelegitimacy_stccol, saudi2_placebo1_stccol)$p.value, ## experiment1
            t.test(uk1_performancelegitimacy_unicefcol, uk1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(uk2_performancelegitimacy_stccol, uk2_placebo1_stccol)$p.value, ## experiment1
            
            t.test(egypt1_performancelegitimacy_unicefcol, egypt1_placebo1_unicefcol)$p.value,## experiment1
            t.test(egypt2_performancelegitimacy_stccol, egypt2_placebo1col)$p.value, ## experiment1
            t.test(egypt_performance_unicef_shortcol, egypt_placebo1col)$p.value, ## experiment2
            
            t.test(Egypt_T11c$clicked, Egypt_T13c$clicked, na.rm = TRUE)$p.value, ## experiment3 (clicks) long unicef
            t.test(Egypt_T11$win_donate_agg, Egypt_T13$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (intention) long unicef
            t.test(Egypt_T11w$howmuch_donate_translated, Egypt_T13w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) long unicef
            t.test(Egypt_T12$win_donate_agg, Egypt_T16$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) short unicef
            t.test(Egypt_T12c$clicked, Egypt_T16c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) short unicef
            t.test(Egypt_T12w$howmuch_donate_translated, Egypt_T16w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) short unicef
            
            t.test(india1_performancelegitimacy_unicefcol, india1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(india2_performancelegitimacy_stccol, india2_placebo1col)$p.value, ## experiment1
            t.test(india_performance_unicef_shortcol, india_placebo1col)$p.value, ## experiment2
            
            t.test(India_T11$win_donate_agg, India_T13$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) long unicef
            t.test(India_T11c$clicked, India_T13c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) long unicef
            t.test(India_T11w$howmuch_donate_translated, India_T13w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) long unicef
            t.test(India_T12$win_donate_agg, India_T16$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) short unicef
            t.test(India_T12c$clicked, India_T16c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) short unicef
            t.test(India_T12w$howmuch_donate_translated, India_T16w$howmuch_donate_translated, na.rm = TRUE)$p.value) ## experiment3 (winnings) short unicef

performance_df <- data.frame(country = countries,
                             test = tests,
                             exptest = experiment.type,
                             treatmentwording = treatment.wording,
                             outcome = outcome, 
                             organization = organization, 
                             year = year,
                             TE = TE,
                             pvalue = pvalue,
                             n = samplesize,
                             seTE = seTE)

performance_df$study <- paste(performance_df$country, 
                              performance_df$test,
                              performance_df$exptest, 
                              performance_df$treatmentwording, 
                              performance_df$outcome, 
                              performance_df$organization, 
                              performance_df$year, 
                              sep = "_")


performance_df_filtered <- subset(performance_df, !(outcome %in% c("winnings", "intention")))


## Multiple meta-regression using metafor (random-effects meta-analysis)
m.all_performance <- rma(yi = TE,
             sei = seTE,
             data = performance_df_filtered,
             method = "ML",
             mods = ~ year + exptest + organization + country + treatmentwording,
             test = "knha")

extract_stats_with_ci <- function(model) {
  coef <- coef(model)
  vcov_matrix <- vcov(model)
  se <- sqrt(diag(vcov_matrix))
  
  ci_lb <- coef - 1.96 * se
  ci_ub <- coef + 1.96 * se
  
  p_value <- model$pval

  aic <- AIC(model)
  bic <- BIC(model)
  
  coef <- format(coef, scientific = FALSE)
  se <- format(se, scientific = FALSE)
  ci_lb <- format(ci_lb, scientific = FALSE)
  ci_ub <- format(ci_ub, scientific = FALSE)
  p_value <- format(p_value, scientific = FALSE)
  aic <- format(aic, scientific = FALSE)
  bic <- format(bic, scientific = FALSE)
  
  stats_df <- data.frame(
    
    Coefficient = coef,
    SE = se,
    CI_lower = ci_lb,
    CI_upper = ci_ub,
    p_value = p_value,
    AIC = aic,
    BIC = bic,
    stringsAsFactors = FALSE
  )
  
  return(stats_df)
}

stats_all_performance <- extract_stats_with_ci(m.all_performance)

variable <- c("intrcpt", "year ", "round",
                 "organization", "egypt", "india", 
                 "saudiarabia", "unitedkingdom", "length")
stats_all_performance$Name <- variable


## Procedural legitimacy
tests = c(rep("procedural", 24)) 

experiment.type <- c("abtest_r1", "abtest_r1",
                     "abtest_r1", "abtest_r1",
                     "abtest_r1", "abtest_r1", 
                     "abtest_r1", "abtest_r1", "abtest_r2", "survey", "survey", "survey", "survey","survey", "survey",
                     "abtest_r1", "abtest_r1", "abtest_r2", "survey", "survey", "survey", "survey","survey", "survey")

treatment.wording <- c("long", "long",
                       "long", "long",
                       "long", "long",
                       "long", "long", "short", "long", "long", "long", "short", "short","short",
                       "long", "long", "short", "long", "long", "long", "short", "short","short")

outcome <- c("clicks", "clicks", 
             "clicks", "clicks", 
             "clicks", "clicks", 
             "clicks", "clicks", "clicks", "clicks", "intention", "winnings", "clicks", "intention", "winnings",
             "clicks", "clicks", "clicks", "clicks", "intention", "winnings", "clicks", "intention", "winnings")

organization <- c("IGO", "NGO",
                  "IGO", "NGO",
                  "IGO", "NGO",
                  "IGO", "NGO", "IGO", "IGO", "IGO", "IGO",  "IGO", "IGO", "IGO",
                  "IGO", "NGO", "IGO", "IGO", "IGO", "IGO",  "IGO", "IGO", "IGO")

year <- c("2022", "2022",
          "2022", "2022",
          "2022", "2022",
          "2022", "2022", "2023", "2023", "2023", "2023", "2023", "2023", "2023",
          "2022", "2022", "2023", "2023", "2023", "2023", "2023", "2023", "2023")

samplesize <- c(length(brazil1_procedurallegitimacy_unicefcol)+length(brazil1_placebo1_unicefcol), ## experiment1
                length(brazil2_procedurallegitimacy_stccol)+length(brazil2_placebo1_stccol), ## experiment1
                
                length(saudi1_procedurallegitimacy_unicefcol)+length(saudi1_placebo1_unicefcol), ## experiment1
                length(saudi2_procedurallegitimacy_stccol)+length(saudi2_placebo1_stccol), ## experiment1
                
                length(uk1_procedurallegitimacy_unicefcol)+length(uk1_placebo1_unicefcol), ## experiment1
                length(uk2_procedurallegitimacy_stccol)+length(uk2_placebo1_stccol), ## experiment1
                
                length(egypt1_procedurallegitimacy_unicefcol)+length(egypt1_placebo1_unicefcol), ## experiment1
                length(egypt2_procedurallegitimacy_stccol)+length(egypt2_placebo1col), ## experiment1
                length(egypt_procedural_unicef_shortcol)+length(egypt_placebo1col), ## experiment2
                
                length(Egypt_T7c$clicked[!is.na(Egypt_T7c$clicked)])+length(Egypt_T13c$clicked[!is.na(Egypt_T13c$clicked)]), ## experiment3 (clicks) long unicef
                length(Egypt_T7$win_donate_agg[!is.na(Egypt_T7$win_donate_agg)])+length(Egypt_T13$win_donate_agg[!is.na(Egypt_T13$win_donate_agg)]), ## experiment3 (intention) long unicef
                length(Egypt_T7w$howmuch_donate_translated[!is.na(Egypt_T7w$howmuch_donate_translated)])+length(Egypt_T13w$howmuch_donate_translated[!is.na(Egypt_T13w$howmuch_donate_translated)]), ## experiment3 (winnings) long unicef
                length(Egypt_T8$win_donate_agg[!is.na(Egypt_T8$win_donate_agg)])+length(Egypt_T16$win_donate_agg[!is.na(Egypt_T16$win_donate_agg)]), ## experiment3 (clicks) short unicef
                length(Egypt_T8c$clicked[!is.na(Egypt_T8c$clicked)])+length(Egypt_T16c$clicked[!is.na(Egypt_T16c$clicked)]), ## experiment3 (intention) short unicef
                length(Egypt_T8w$howmuch_donate_translated[!is.na(Egypt_T8w$howmuch_donate_translated)])+length(Egypt_T16w$howmuch_donate_translated[!is.na(Egypt_T16w$howmuch_donate_translated)]), ## experiment3 (winnings) short unicef
                
                length(india1_procedurallegitimacy_unicefcol)+length(india1_placebo1_unicefcol), ## experiment1
                length(india2_procedurallegitimacy_stccol)+length(india2_placebo1col), ## experiment1
                length(india_procedural_unicef_shortcol)+length(india_placebo1col), ## experiment2
                length(India_T7$win_donate_agg[!is.na(India_T7$win_donate_agg)])+length(India_T13$win_donate_agg[!is.na(India_T13$win_donate_agg)]), ## experiment3 (clicks) long unicef
                length(India_T7c$clicked[!is.na(India_T7c$clicked)])+length(India_T13c$clicked[!is.na(India_T13c$clicked)]), ## experiment3 (intention) long unicef
                length(India_T7w$howmuch_donate_translated[!is.na(India_T7w$howmuch_donate_translated)])+length(India_T13w$howmuch_donate_translated[!is.na(India_T13w$howmuch_donate_translated)]), ## experiment3 (winnings) long unicef
                length(India_T8$win_donate_agg[!is.na(India_T8$win_donate_agg)])+length(India_T16$win_donate_agg[!is.na(India_T16$win_donate_agg)]), ## experiment3 (clicks) short unicef
                length(India_T8c$clicked[!is.na(India_T8c$clicked)])+length(India_T16c$clicked[!is.na(India_T16c$clicked)]), ## experiment3 (intention) short unicef
                length(India_T8w$howmuch_donate_translated[!is.na(India_T8w$howmuch_donate_translated)])+length(India_T16w$howmuch_donate_translated[!is.na(India_T16w$howmuch_donate_translated)]) ## experiment3 (winnings) short unicef
) 


countries <- c(rep("Brazil", 2), rep("Saudi Arabia", 2), rep("United Kingdom", 2), rep("Egypt", 9), rep("India", 9))

TE <- c(mean(brazil1_procedurallegitimacy_unicefcol)-mean(brazil1_placebo1_unicefcol), ## experiment1
        mean(brazil2_procedurallegitimacy_stccol)-mean(brazil2_placebo1_stccol), ## experiment1
        
        mean(saudi1_procedurallegitimacy_unicefcol)-mean(saudi1_placebo1_unicefcol), ## experiment1
        mean(saudi2_procedurallegitimacy_stccol)-mean(saudi2_placebo1_stccol), ## experiment1
        
        mean(uk1_procedurallegitimacy_unicefcol)-mean(uk1_placebo1_unicefcol), ## experiment1
        mean(uk2_procedurallegitimacy_stccol)-mean(uk2_placebo1_stccol), ## experiment1
        
        mean(egypt1_procedurallegitimacy_unicefcol)-mean(egypt1_placebo1_unicefcol), ## experiment1
        mean(egypt2_procedurallegitimacy_stccol)-mean(egypt2_placebo1col), ## experiment1
        mean(egypt_procedural_unicef_shortcol)-mean(egypt_placebo1col), ## experiment2
        
        mean(Egypt_T7c$clicked, na.rm = TRUE)-mean(Egypt_T13c$clicked, na.rm = TRUE), ## experiment3 (clicks) long unicef
        mean(Egypt_T7$win_donate_agg, na.rm = TRUE)-mean(Egypt_T13$win_donate_agg, na.rm = TRUE), ## experiment3 (intention) long unicef
        mean(Egypt_T7w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) long unicef
        mean(Egypt_T8$win_donate_agg, na.rm = TRUE)-mean(Egypt_T16$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) short unicef
        mean(Egypt_T8c$clicked, na.rm = TRUE)-mean(Egypt_T16c$clicked, na.rm = TRUE), ## experiment3 (intention) short unicef
        mean(Egypt_T8w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) short unicef
        
        mean(india1_procedurallegitimacy_unicefcol)-mean(india1_placebo1_unicefcol), ## experiment1
        mean(india2_procedurallegitimacy_stccol)-mean(india2_placebo1col), ## experiment1
        mean(india_procedural_unicef_shortcol)-mean(india_placebo1col), ## experiment2
        
        mean(India_T7$win_donate_agg, na.rm = TRUE)-mean(India_T13$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) long unicef
        mean(India_T7c$clicked, na.rm = TRUE)-mean(India_T13c$clicked, na.rm = TRUE), ## experiment3 (intention) long unicef
        mean(India_T7w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T13w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) long unicef
        mean(India_T8$win_donate_agg, na.rm = TRUE)-mean(India_T16$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) short unicef
        mean(India_T8c$clicked, na.rm = TRUE)-mean(India_T16c$clicked, na.rm = TRUE), ## experiment3 (intention) short unicef
        mean(India_T8w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef
) 


brazil_abtest_r1_procedural_unicef_tt <- t.test(brazil1_procedurallegitimacy_unicefcol, brazil1_placebo1_unicefcol) ## experiment1
brazil_abtest_r1_procedural_stc_tt <- t.test(brazil2_procedurallegitimacy_stccol, brazil2_placebo1_stccol) ## experiment1

sa_abtest_r1_procedural_unicef_tt <- t.test(saudi1_procedurallegitimacy_unicefcol, saudi1_placebo1_unicefcol) ## experiment1
sa_abtest_r1_procedural_stc_tt <- t.test(saudi2_procedurallegitimacy_stccol, saudi2_placebo1_stccol) ## experiment1

uk_abtest_r1_procedural_unicef_tt <- t.test(uk1_procedurallegitimacy_unicefcol, uk1_placebo1_unicefcol) ## experiment1
uk_abtest_r1_procedural_stc_tt <- t.test(uk2_procedurallegitimacy_stccol, uk2_placebo1_stccol) ## experiment1

egypt_abtest_r1_procedural_unicef_tt <- t.test(egypt1_procedurallegitimacy_unicefcol, egypt1_placebo1_unicefcol) ## experiment1
egypt_abtest_r1_procedural_stc_tt <- t.test(egypt2_procedurallegitimacy_stccol, egypt2_placebo1col) ## experiment1
egypt_abtest_r2_procedural_uniceflong_tt <- t.test(egypt_procedural_unicef_shortcol, egypt_placebo1col) ## experiment2

egypt_survey_procedural_uniceflong_click_tt <- t.test(Egypt_T7c$clicked, Egypt_T13c$clicked, na.rm = TRUE) ## experiment3 (clicks) long unicef
egypt_survey_procedural_uniceflong_intention_tt <- t.test(Egypt_T7$win_donate_agg, Egypt_T13$win_donate_agg, na.rm = TRUE) ## experiment3 (intention) long unicef
egypt_survey_procedural_uniceflong_winning_tt <- t.test(Egypt_T7w$howmuch_donate_translated, Egypt_T13w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) long unicef
egypt_survey_procedural_unicefshort_click_tt <- t.test(Egypt_T8$win_donate_agg, Egypt_T16$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) short unicef
egypt_survey_procedural_unicefshort_intention_tt <- t.test(Egypt_T8c$clicked, Egypt_T16c$clicked, na.rm = TRUE) ## experiment3 (intention) short unicef
egypt_survey_procedural_unicefshort_winning_tt <- t.test(Egypt_T8w$howmuch_donate_translated, Egypt_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef

india_abtest_r1_procedural_unicef_tt <- t.test(india1_procedurallegitimacy_unicefcol, india1_placebo1_unicefcol) ## experiment1
india_abtest_r1_procedural_stc_tt <- t.test(india2_procedurallegitimacy_stccol, india2_placebo1col) ## experiment1
india_abtest_r2_procedural_uniceflong_tt <- t.test(india_procedural_unicef_shortcol, india_placebo1col) ## experiment2

india_survey_procedural_uniceflong_click_tt <- t.test(India_T7$win_donate_agg, India_T13$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) long unicef
india_survey_procedural_uniceflong_intention_tt <-t.test(India_T7c$clicked, India_T13c$clicked, na.rm = TRUE) ## experiment3 (intention) long unicef
india_survey_procedural_uniceflong_winning_tt <- t.test(India_T7w$howmuch_donate_translated, India_T13w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) long unicef
india_survey_procedural_unicefshort_click_tt <- t.test(India_T8$win_donate_agg, India_T16$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) short unicef
india_survey_procedural_unicefshort_intention_tt <- t.test(India_T8c$clicked, India_T16c$clicked, na.rm = TRUE) ## experiment3 (intention) short unicef
india_survey_procedural_unicefshort_winning_tt <- t.test(India_T8w$howmuch_donate_translated, India_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef


brazil_abtest_r1_procedural_unicef_tt_se <- sqrt(brazil_abtest_r1_procedural_unicef_tt$stderr)
brazil_abtest_r1_procedural_stc_tt_se <- sqrt(brazil_abtest_r1_procedural_stc_tt$stderr)

sa_abtest_r1_procedural_unicef_tt_se <- sqrt(sa_abtest_r1_procedural_unicef_tt$stderr)
sa_abtest_r1_procedural_stc_tt_se <- sqrt(sa_abtest_r1_procedural_stc_tt$stderr)

uk_abtest_r1_procedural_unicef_tt_se <- sqrt(uk_abtest_r1_procedural_unicef_tt$stderr)
uk_abtest_r1_procedural_stc_tt_se <- sqrt(uk_abtest_r1_procedural_stc_tt$stderr)

egypt_abtest_r1_procedural_unicef_tt_se <- sqrt(egypt_abtest_r1_procedural_unicef_tt$stderr)
egypt_abtest_r1_procedural_stc_tt_se <- sqrt(egypt_abtest_r1_procedural_stc_tt$stderr)
egypt_abtest_r2_procedural_uniceflong_tt_se <- sqrt(egypt_abtest_r2_procedural_uniceflong_tt$stderr)
egypt_survey_procedural_uniceflong_click_tt_se <- sqrt(egypt_survey_procedural_uniceflong_click_tt$stderr)
egypt_survey_procedural_uniceflong_intention_tt_se <- sqrt(egypt_survey_procedural_uniceflong_intention_tt$stderr)
egypt_survey_procedural_uniceflong_winning_tt_se <- sqrt(egypt_survey_procedural_uniceflong_winning_tt$stderr)
egypt_survey_procedural_unicefshort_click_tt_se <- sqrt(egypt_survey_procedural_unicefshort_click_tt$stderr)
egypt_survey_procedural_unicefshort_intention_tt_se <- sqrt(egypt_survey_procedural_unicefshort_intention_tt$stderr)
egypt_survey_procedural_unicefshort_winning_tt_se <- sqrt(egypt_survey_procedural_unicefshort_winning_tt$stderr)

india_abtest_r1_procedural_unicef_tt_se <- sqrt(india_abtest_r1_procedural_unicef_tt$stderr)
india_abtest_r1_procedural_stc_tt_se <- sqrt(india_abtest_r1_procedural_stc_tt$stderr)
india_abtest_r2_procedural_uniceflong_tt_se <- sqrt(india_abtest_r2_procedural_uniceflong_tt$stderr)
india_survey_procedural_uniceflong_click_tt_se <- sqrt(india_survey_procedural_uniceflong_click_tt$stderr)
india_survey_procedural_uniceflong_intention_tt_se <- sqrt(india_survey_procedural_uniceflong_intention_tt$stderr)
india_survey_procedural_uniceflong_winning_tt_se <- sqrt(india_survey_procedural_uniceflong_winning_tt$stderr)
india_survey_procedural_unicefshort_click_tt_se <- sqrt(india_survey_procedural_unicefshort_click_tt$stderr)
india_survey_procedural_unicefshort_intention_tt_se <- sqrt(india_survey_procedural_unicefshort_intention_tt$stderr)
india_survey_procedural_unicefshort_winning_tt_se <- sqrt(india_survey_procedural_unicefshort_winning_tt$stderr)

seTE <- c(brazil_abtest_r1_procedural_unicef_tt_se, brazil_abtest_r1_procedural_stc_tt_se,
          sa_abtest_r1_procedural_unicef_tt_se, sa_abtest_r1_procedural_stc_tt_se,
          uk_abtest_r1_procedural_unicef_tt_se, uk_abtest_r1_procedural_stc_tt_se,
          egypt_abtest_r1_procedural_unicef_tt_se, egypt_abtest_r1_procedural_stc_tt_se,
          egypt_abtest_r2_procedural_uniceflong_tt_se,
          egypt_survey_procedural_uniceflong_click_tt_se, egypt_survey_procedural_uniceflong_intention_tt_se, egypt_survey_procedural_uniceflong_winning_tt_se,
          egypt_survey_procedural_unicefshort_click_tt_se, egypt_survey_procedural_unicefshort_intention_tt_se, egypt_survey_procedural_unicefshort_winning_tt_se,
          
          india_abtest_r1_procedural_unicef_tt_se, india_abtest_r1_procedural_stc_tt_se,
          india_abtest_r2_procedural_uniceflong_tt_se,
          india_survey_procedural_uniceflong_click_tt_se, india_survey_procedural_uniceflong_intention_tt_se, india_survey_procedural_uniceflong_winning_tt_se,
          india_survey_procedural_unicefshort_click_tt_se, india_survey_procedural_unicefshort_intention_tt_se, india_survey_procedural_unicefshort_winning_tt_se)

pvalue <- c(t.test(brazil1_procedurallegitimacy_unicefcol, brazil1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(brazil2_procedurallegitimacy_stccol, brazil2_placebo1_stccol)$p.value, ## experiment1
            t.test(saudi1_procedurallegitimacy_unicefcol, saudi1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(saudi2_procedurallegitimacy_stccol, saudi2_placebo1_stccol)$p.value, ## experiment1
            t.test(uk1_procedurallegitimacy_unicefcol, uk1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(uk2_procedurallegitimacy_stccol, uk2_placebo1_stccol)$p.value, ## experiment1
            
            t.test(egypt1_procedurallegitimacy_unicefcol, egypt1_placebo1_unicefcol)$p.value,## experiment1
            t.test(egypt2_procedurallegitimacy_stccol, egypt2_placebo1col)$p.value, ## experiment1
            t.test(egypt_procedural_unicef_shortcol, egypt_placebo1col)$p.value, ## experiment2
            t.test(Egypt_T7c$clicked, Egypt_T13c$clicked, na.rm = TRUE)$p.value, ## experiment3 (clicks) long unicef
            t.test(Egypt_T7$win_donate_agg, Egypt_T13$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (intention) long unicef
            t.test(Egypt_T7w$howmuch_donate_translated, Egypt_T13w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) long unicef
            t.test(Egypt_T8$win_donate_agg, Egypt_T16$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) short unicef
            t.test(Egypt_T8c$clicked, Egypt_T16c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) short unicef
            t.test(Egypt_T8w$howmuch_donate_translated, Egypt_T16w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) short unicef
            
            t.test(india1_procedurallegitimacy_unicefcol, india1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(india2_procedurallegitimacy_stccol, india2_placebo1col)$p.value, ## experiment1
            t.test(india_procedural_unicef_shortcol, india_placebo1col)$p.value, ## experiment2
            t.test(India_T7$win_donate_agg, India_T13$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) long unicef
            t.test(India_T7c$clicked, India_T13c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) long unicef
            t.test(India_T7w$howmuch_donate_translated, India_T13w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) long unicef
            t.test(India_T8$win_donate_agg, India_T16$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) short unicef
            t.test(India_T8c$clicked, India_T16c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) short unicef
            t.test(India_T8w$howmuch_donate_translated, India_T16w$howmuch_donate_translated, na.rm = TRUE)$p.value) ## experiment3 (winnings) short unicef


procedural_df <- data.frame(country = countries,
                            test = tests,
                            exptest = experiment.type,
                            treatmentwording = treatment.wording,
                            outcome = outcome, 
                            organization = organization, 
                            year = year,
                            TE = TE,
                            pvalue = pvalue,
                            n = samplesize,
                            seTE = seTE)

procedural_df$study <- paste(procedural_df$country, 
                             procedural_df$test,
                             procedural_df$exptest, 
                             procedural_df$treatmentwording, 
                             procedural_df$outcome, 
                             procedural_df$organization, 
                             procedural_df$year, 
                             sep = "_")

procedural_df_filtered <- subset(procedural_df, !(outcome %in% c("winnings", "intention")))


## Multiple meta-regression using metafor (random-effects meta-analysis)
m.all_procedural <- rma(yi = TE,
             sei = seTE,
             data = procedural_df_filtered,
             method = "ML",
             mods = ~ year + exptest + organization + country + treatmentwording,
             test = "knha")

stats_all_procedural <- extract_stats_with_ci(m.all_procedural)

variable <- c("intrcpt", "year ", "round",
              "organization", "egypt", "india", 
              "saudiarabia", "unitedkingdom", "length") 
stats_all_procedural$Name <- variable

stats_all_procedural[] <- lapply(stats_all_procedural, function(x) {
  if (is.numeric(x)) round(x, 4) else x
})


## Mandate-based legitimacy (Norm)
tests = c(rep("norm", 24)) 

experiment.type <- c("abtest_r1", "abtest_r1",
                     "abtest_r1", "abtest_r1",
                     "abtest_r1", "abtest_r1", 
                     "abtest_r1", "abtest_r1", "abtest_r2", "survey", "survey", "survey", "survey","survey", "survey",
                     "abtest_r1", "abtest_r1", "abtest_r2", "survey", "survey", "survey", "survey","survey", "survey")

treatment.wording <- c("long", "long",
                       "long", "long",
                       "long", "long",
                       "long", "long", "short", "long", "long", "long", "short", "short","short",
                       "long", "long", "short", "long", "long", "long", "short", "short","short")

outcome <- c("clicks", "clicks", 
             "clicks", "clicks", 
             "clicks", "clicks", 
             "clicks", "clicks", "clicks", "clicks", "intention", "winnings", "clicks", "intention", "winnings",
             "clicks", "clicks", "clicks", "clicks", "intention", "winnings", "clicks", "intention", "winnings")

organization <- c("IGO", "NGO",
                  "IGO", "NGO",
                  "IGO", "NGO",
                  "IGO", "NGO", "IGO", "IGO", "IGO", "IGO",  "IGO", "IGO", "IGO",
                  "IGO", "NGO", "IGO", "IGO", "IGO", "IGO",  "IGO", "IGO", "IGO")

year <- c("2022", "2022",
          "2022", "2022",
          "2022", "2022",
          "2022", "2022", "2023", "2023", "2023", "2023", "2023", "2023", "2023",
          "2022", "2022", "2023", "2023", "2023", "2023", "2023", "2023", "2023")

samplesize <- c(length(brazil1_normlegitimacy_unicefcol)+length(brazil1_placebo1_unicefcol), ## experiment1
                length(brazil2_normlegitimacy_stccol)+length(brazil2_placebo1_stccol), ## experiment1
                
                length(saudi1_normlegitimacy_unicefcol)+length(saudi1_placebo1_unicefcol), ## experiment1
                length(saudi2_normlegitimacy_stccol)+length(saudi2_placebo1_stccol), ## experiment1
                
                length(uk1_normlegitimacy_unicefcol)+length(uk1_placebo1_unicefcol), ## experiment1
                length(uk2_normlegitimacy_stccol)+length(uk2_placebo1_stccol), ## experiment1
                
                length(egypt1_normlegitimacy_unicefcol)+length(egypt1_placebo1_unicefcol), ## experiment1
                length(egypt2_normlegitimacy_stccol)+length(egypt2_placebo1col), ## experiment1
                length(egypt_norm_unicef_shortcol)+length(egypt_placebo1col), ## experiment2
                
                length(Egypt_T9c$clicked[!is.na(Egypt_T9c$clicked)])+length(Egypt_T13c$clicked[!is.na(Egypt_T13c$clicked)]), ## experiment3 (clicks) long unicef
                length(Egypt_T9$win_donate_agg[!is.na(Egypt_T9$win_donate_agg)])+length(Egypt_T13$win_donate_agg[!is.na(Egypt_T13$win_donate_agg)]), ## experiment3 (intention) long unicef
                length(Egypt_T9w$howmuch_donate_translated[!is.na(Egypt_T9w$howmuch_donate_translated)])+length(Egypt_T13w$howmuch_donate_translated[!is.na(Egypt_T13w$howmuch_donate_translated)]), ## experiment3 (winnings) long unicef
                length(Egypt_T10$win_donate_agg[!is.na(Egypt_T10$win_donate_agg)])+length(Egypt_T16$win_donate_agg[!is.na(Egypt_T16$win_donate_agg)]), ## experiment3 (clicks) short unicef
                length(Egypt_T10c$clicked[!is.na(Egypt_T10c$clicked)])+length(Egypt_T16c$clicked[!is.na(Egypt_T16c$clicked)]), ## experiment3 (intention) short unicef
                length(Egypt_T10w$howmuch_donate_translated[!is.na(Egypt_T10w$howmuch_donate_translated)])+length(Egypt_T16w$howmuch_donate_translated[!is.na(Egypt_T16w$howmuch_donate_translated)]), ## experiment3 (winnings) short unicef
                
                length(india1_normlegitimacy_unicefcol)+length(india1_placebo1_unicefcol), ## experiment1
                length(india2_normlegitimacy_stccol)+length(india2_placebo1col), ## experiment1
                length(india_norm_unicef_shortcol)+length(india_placebo1col), ## experiment2
                
                length(India_T9$win_donate_agg[!is.na(India_T9$win_donate_agg)])+length(India_T13$win_donate_agg[!is.na(India_T13$win_donate_agg)]), ## experiment3 (clicks) long unicef
                length(India_T9c$clicked[!is.na(India_T9c$clicked)])+length(India_T13c$clicked[!is.na(India_T13c$clicked)]), ## experiment3 (intention) long unicef
                length(India_T9w$howmuch_donate_translated[!is.na(India_T9w$howmuch_donate_translated)])+length(India_T13w$howmuch_donate_translated[!is.na(India_T13w$howmuch_donate_translated)]), ## experiment3 (winnings) long unicef
                length(India_T10$win_donate_agg[!is.na(India_T10$win_donate_agg)])+length(India_T16$win_donate_agg[!is.na(India_T16$win_donate_agg)]), ## experiment3 (clicks) short unicef
                length(India_T10c$clicked[!is.na(India_T10c$clicked)])+length(India_T16c$clicked[!is.na(India_T16c$clicked)]), ## experiment3 (intention) short unicef
                length(India_T10w$howmuch_donate_translated[!is.na(India_T10w$howmuch_donate_translated)])+length(India_T16w$howmuch_donate_translated[!is.na(India_T16w$howmuch_donate_translated)]) ## experiment3 (winnings) short unicef
) 

countries <- c(rep("Brazil", 2), rep("Saudi Arabia", 2), rep("United Kingdom", 2), rep("Egypt", 9), rep("India", 9))

TE <- c(mean(brazil1_normlegitimacy_unicefcol)-mean(brazil1_placebo1_unicefcol), ## experiment1
        mean(brazil2_normlegitimacy_stccol)-mean(brazil2_placebo1_stccol), ## experiment1
        
        mean(saudi1_normlegitimacy_unicefcol)-mean(saudi1_placebo1_unicefcol), ## experiment1
        mean(saudi2_normlegitimacy_stccol)-mean(saudi2_placebo1_stccol), ## experiment1
        
        mean(uk1_normlegitimacy_unicefcol)-mean(uk1_placebo1_unicefcol), ## experiment1
        mean(uk2_normlegitimacy_stccol)-mean(uk2_placebo1_stccol), ## experiment1
        
        mean(egypt1_normlegitimacy_unicefcol)-mean(egypt1_placebo1_unicefcol), ## experiment1
        mean(egypt2_normlegitimacy_stccol)-mean(egypt2_placebo1col), ## experiment1
        mean(egypt_norm_unicef_shortcol)-mean(egypt_placebo1col), ## experiment2
        
        mean(Egypt_T9c$clicked, na.rm = TRUE)-mean(Egypt_T13c$clicked, na.rm = TRUE), ## experiment3 (clicks) long unicef
        mean(Egypt_T9$win_donate_agg, na.rm = TRUE)-mean(Egypt_T13$win_donate_agg, na.rm = TRUE), ## experiment3 (intention) long unicef
        mean(Egypt_T9w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T13w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) long unicef
        mean(Egypt_T10$win_donate_agg, na.rm = TRUE)-mean(Egypt_T16$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) short unicef
        mean(Egypt_T10c$clicked, na.rm = TRUE)-mean(Egypt_T16c$clicked, na.rm = TRUE), ## experiment3 (intention) short unicef
        mean(Egypt_T10w$howmuch_donate_translated, na.rm = TRUE)-mean(Egypt_T16w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) short unicef
        
        mean(india1_normlegitimacy_unicefcol)-mean(india1_placebo1_unicefcol), ## experiment1
        mean(india2_normlegitimacy_stccol)-mean(india2_placebo1col), ## experiment1
        mean(india_norm_unicef_shortcol)-mean(india_placebo1col), ## experiment2
        
        mean(India_T9$win_donate_agg, na.rm = TRUE)-mean(India_T13$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) long unicef
        mean(India_T9c$clicked, na.rm = TRUE)-mean(India_T13c$clicked, na.rm = TRUE), ## experiment3 (intention) long unicef
        mean(India_T9w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T13w$howmuch_donate_translated, na.rm = TRUE), ## experiment3 (winnings) long unicef
        mean(India_T10$win_donate_agg, na.rm = TRUE)-mean(India_T16$win_donate_agg, na.rm = TRUE), ## experiment3 (clicks) short unicef
        mean(India_T10c$clicked, na.rm = TRUE)-mean(India_T16c$clicked, na.rm = TRUE), ## experiment3 (intention) short unicef
        mean(India_T10w$howmuch_donate_translated, na.rm = TRUE)-mean(India_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef
) 


brazil_abtest_r1_norm_unicef_tt <- t.test(brazil1_normlegitimacy_unicefcol, brazil1_placebo1_unicefcol) ## experiment1
brazil_abtest_r1_norm_stc_tt <- t.test(brazil2_normlegitimacy_stccol, brazil2_placebo1_stccol) ## experiment1

sa_abtest_r1_norm_unicef_tt <- t.test(saudi1_normlegitimacy_unicefcol, saudi1_placebo1_unicefcol) ## experiment1
sa_abtest_r1_norm_stc_tt <- t.test(saudi2_normlegitimacy_stccol, saudi2_placebo1_stccol) ## experiment1

uk_abtest_r1_norm_unicef_tt <- t.test(uk1_normlegitimacy_unicefcol, uk1_placebo1_unicefcol) ## experiment1
uk_abtest_r1_norm_stc_tt <- t.test(uk2_normlegitimacy_stccol, uk2_placebo1_stccol) ## experiment1

egypt_abtest_r1_norm_unicef_tt <- t.test(egypt1_normlegitimacy_unicefcol, egypt1_placebo1_unicefcol) ## experiment1
egypt_abtest_r1_norm_stc_tt <- t.test(egypt2_normlegitimacy_stccol, egypt2_placebo1col) ## experiment1
egypt_abtest_r2_norm_uniceflong_tt <- t.test(egypt_norm_unicef_shortcol, egypt_placebo1col) ## experiment2
egypt_survey_norm_uniceflong_click_tt <- t.test(Egypt_T9c$clicked, Egypt_T13c$clicked, na.rm = TRUE) ## experiment3 (clicks) long unicef
egypt_survey_norm_uniceflong_intention_tt <- t.test(Egypt_T9$win_donate_agg, Egypt_T13$win_donate_agg, na.rm = TRUE) ## experiment3 (intention) long unicef
egypt_survey_norm_uniceflong_winning_tt <- t.test(Egypt_T9w$howmuch_donate_translated, Egypt_T13w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) long unicef
egypt_survey_norm_unicefshort_click_tt <- t.test(Egypt_T10$win_donate_agg, Egypt_T16$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) short unicef
egypt_survey_norm_unicefshort_intention_tt <- t.test(Egypt_T10c$clicked, Egypt_T16c$clicked, na.rm = TRUE) ## experiment3 (intention) short unicef
egypt_survey_norm_unicefshort_winning_tt <- t.test(Egypt_T10w$howmuch_donate_translated, Egypt_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef

india_abtest_r1_norm_unicef_tt <- t.test(india1_normlegitimacy_unicefcol, india1_placebo1_unicefcol) ## experiment1
india_abtest_r1_norm_stc_tt <- t.test(india2_normlegitimacy_stccol, india2_placebo1col) ## experiment1
india_abtest_r2_norm_uniceflong_tt <- t.test(india_norm_unicef_shortcol, india_placebo1col) ## experiment2
india_survey_norm_uniceflong_click_tt <- t.test(India_T9$win_donate_agg, India_T13$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) long unicef
india_survey_norm_uniceflong_intention_tt <-t.test(India_T9c$clicked, India_T13c$clicked, na.rm = TRUE) ## experiment3 (intention) long unicef
india_survey_norm_uniceflong_winning_tt <- t.test(India_T9w$howmuch_donate_translated, India_T13w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) long unicef
india_survey_norm_unicefshort_click_tt <- t.test(India_T10$win_donate_agg, India_T16$win_donate_agg, na.rm = TRUE) ## experiment3 (clicks) short unicef
india_survey_norm_unicefshort_intention_tt <- t.test(India_T10c$clicked, India_T16c$clicked, na.rm = TRUE) ## experiment3 (intention) short unicef
india_survey_norm_unicefshort_winning_tt <- t.test(India_T10w$howmuch_donate_translated, India_T16w$howmuch_donate_translated, na.rm = TRUE) ## experiment3 (winnings) short unicef

brazil_abtest_r1_norm_unicef_tt_se <- sqrt(brazil_abtest_r1_norm_unicef_tt$stderr)
brazil_abtest_r1_norm_stc_tt_se <- sqrt(brazil_abtest_r1_norm_stc_tt$stderr)

sa_abtest_r1_norm_unicef_tt_se <- sqrt(sa_abtest_r1_norm_unicef_tt$stderr)
sa_abtest_r1_norm_stc_tt_se <- sqrt(sa_abtest_r1_norm_stc_tt$stderr)

uk_abtest_r1_norm_unicef_tt_se <- sqrt(uk_abtest_r1_norm_unicef_tt$stderr)
uk_abtest_r1_norm_stc_tt_se <- sqrt(uk_abtest_r1_norm_stc_tt$stderr)

egypt_abtest_r1_norm_unicef_tt_se <- sqrt(egypt_abtest_r1_norm_unicef_tt$stderr)
egypt_abtest_r1_norm_stc_tt_se <- sqrt(egypt_abtest_r1_norm_stc_tt$stderr)
egypt_abtest_r2_norm_uniceflong_tt_se <- sqrt(egypt_abtest_r2_norm_uniceflong_tt$stderr)
egypt_survey_norm_uniceflong_click_tt_se <- sqrt(egypt_survey_norm_uniceflong_click_tt$stderr)
egypt_survey_norm_uniceflong_intention_tt_se <- sqrt(egypt_survey_norm_uniceflong_intention_tt$stderr)
egypt_survey_norm_uniceflong_winning_tt_se <- sqrt(egypt_survey_norm_uniceflong_winning_tt$stderr)
egypt_survey_norm_unicefshort_click_tt_se <- sqrt(egypt_survey_norm_unicefshort_click_tt$stderr)
egypt_survey_norm_unicefshort_intention_tt_se <- sqrt(egypt_survey_norm_unicefshort_intention_tt$stderr)
egypt_survey_norm_unicefshort_winning_tt_se <- sqrt(egypt_survey_norm_unicefshort_winning_tt$stderr)

india_abtest_r1_norm_unicef_tt_se <- sqrt(india_abtest_r1_norm_unicef_tt$stderr)
india_abtest_r1_norm_stc_tt_se <- sqrt(india_abtest_r1_norm_stc_tt$stderr)
india_abtest_r2_norm_uniceflong_tt_se <- sqrt(india_abtest_r2_norm_uniceflong_tt$stderr)
india_survey_norm_uniceflong_click_tt_se <- sqrt(india_survey_norm_uniceflong_click_tt$stderr)
india_survey_norm_uniceflong_intention_tt_se <- sqrt(india_survey_norm_uniceflong_intention_tt$stderr)
india_survey_norm_uniceflong_winning_tt_se <- sqrt(india_survey_norm_uniceflong_winning_tt$stderr)
india_survey_norm_unicefshort_click_tt_se <- sqrt(india_survey_norm_unicefshort_click_tt$stderr)
india_survey_norm_unicefshort_intention_tt_se <- sqrt(india_survey_norm_unicefshort_intention_tt$stderr)
india_survey_norm_unicefshort_winning_tt_se <- sqrt(india_survey_norm_unicefshort_winning_tt$stderr)

seTE <- c(brazil_abtest_r1_norm_unicef_tt_se, brazil_abtest_r1_norm_stc_tt_se,
          sa_abtest_r1_norm_unicef_tt_se, sa_abtest_r1_norm_stc_tt_se,
          uk_abtest_r1_norm_unicef_tt_se, uk_abtest_r1_norm_stc_tt_se,
          egypt_abtest_r1_norm_unicef_tt_se, egypt_abtest_r1_norm_stc_tt_se,
          egypt_abtest_r2_norm_uniceflong_tt_se,
          egypt_survey_norm_uniceflong_click_tt_se, egypt_survey_norm_uniceflong_intention_tt_se, egypt_survey_norm_uniceflong_winning_tt_se,
          egypt_survey_norm_unicefshort_click_tt_se, egypt_survey_norm_unicefshort_intention_tt_se, egypt_survey_norm_unicefshort_winning_tt_se,
          
          india_abtest_r1_norm_unicef_tt_se, india_abtest_r1_norm_stc_tt_se,
          india_abtest_r2_norm_uniceflong_tt_se,
          india_survey_norm_uniceflong_click_tt_se, india_survey_norm_uniceflong_intention_tt_se, india_survey_norm_uniceflong_winning_tt_se,
          india_survey_norm_unicefshort_click_tt_se, india_survey_norm_unicefshort_intention_tt_se, india_survey_norm_unicefshort_winning_tt_se)

pvalue <- c(t.test(brazil1_normlegitimacy_unicefcol, brazil1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(brazil2_normlegitimacy_stccol, brazil2_placebo1_stccol)$p.value, ## experiment1
            t.test(saudi1_normlegitimacy_unicefcol, saudi1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(saudi2_normlegitimacy_stccol, saudi2_placebo1_stccol)$p.value, ## experiment1
            t.test(uk1_normlegitimacy_unicefcol, uk1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(uk2_normlegitimacy_stccol, uk2_placebo1_stccol)$p.value, ## experiment1
            
            t.test(egypt1_normlegitimacy_unicefcol, egypt1_placebo1_unicefcol)$p.value,## experiment1
            t.test(egypt2_normlegitimacy_stccol, egypt2_placebo1col)$p.value, ## experiment1
            t.test(egypt_norm_unicef_shortcol, egypt_placebo1col)$p.value, ## experiment2
            t.test(Egypt_T9c$clicked, Egypt_T13c$clicked, na.rm = TRUE)$p.value, ## experiment3 (clicks) long unicef
            t.test(Egypt_T9$win_donate_agg, Egypt_T13$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (intention) long unicef
            t.test(Egypt_T9w$howmuch_donate_translated, Egypt_T13w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) long unicef
            t.test(Egypt_T10$win_donate_agg, Egypt_T16$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) short unicef
            t.test(Egypt_T10c$clicked, Egypt_T16c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) short unicef
            t.test(Egypt_T10w$howmuch_donate_translated, Egypt_T16w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) short unicef
            
            t.test(india1_normlegitimacy_unicefcol, india1_placebo1_unicefcol)$p.value, ## experiment1
            t.test(india2_normlegitimacy_stccol, india2_placebo1col)$p.value, ## experiment1
            t.test(india_norm_unicef_shortcol, india_placebo1col)$p.value, ## experiment2
            t.test(India_T9$win_donate_agg, India_T13$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) long unicef
            t.test(India_T9c$clicked, India_T13c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) long unicef
            t.test(India_T9w$howmuch_donate_translated, India_T13w$howmuch_donate_translated, na.rm = TRUE)$p.value, ## experiment3 (winnings) long unicef
            t.test(India_T10$win_donate_agg, India_T16$win_donate_agg, na.rm = TRUE)$p.value, ## experiment3 (clicks) short unicef
            t.test(India_T10c$clicked, India_T16c$clicked, na.rm = TRUE)$p.value, ## experiment3 (intention) short unicef
            t.test(India_T10w$howmuch_donate_translated, India_T16w$howmuch_donate_translated, na.rm = TRUE)$p.value) ## experiment3 (winnings) short unicef

norm_df <- data.frame(country = countries,
                      test = tests,
                      exptest = experiment.type,
                      treatmentwording = treatment.wording,
                      outcome = outcome, 
                      organization = organization, 
                      year = year,
                      TE = TE,
                      pvalue = pvalue,
                      n = samplesize,
                      seTE = seTE)

norm_df$study <- paste(norm_df$country, 
                       norm_df$test,
                       norm_df$exptest, 
                       norm_df$treatmentwording, 
                       norm_df$outcome, 
                       norm_df$organization, 
                       norm_df$year, 
                       sep = "_")


norm_df_filtered <- subset(norm_df, !(outcome %in% c("winnings", "intention")))


## Multiple meta-regression using metafor (random-effects meta-analysis)
m.all_norm <- rma(yi = TE,
             sei = seTE,
             data = norm_df_filtered,
             method = "ML",
             mods = ~ year + exptest + organization + country + treatmentwording,
             test = "knha")

stats_all_norm <- extract_stats_with_ci(m.all_norm)

variable <- c("intrcpt", "year ", "round",
              "organization", "egypt", "india", 
              "saudiarabia", "unitedkingdom", "length") 

stats_all_norm$Name <- variable

stats_all_norm[] <- lapply(stats_all_norm, function(x) {
  if (is.numeric(x)) round(x, 4) else x
})



## Combined (all legitimacy)
performance_df_filtered$legitimacy_type <- "performance"
procedural_df_filtered$legitimacy_type <- "procedural"
norm_df_filtered$legitimacy_type <- "norm"

all_df <- rbind(performance_df_filtered, procedural_df_filtered, norm_df_filtered)

m.full <- rma(yi = TE,
              sei = seTE,
              data = all_df,
              method = "ML",
              mods = ~ legitimacy_type + year + exptest + organization + country + treatmentwording,
              test = "knha")

stats_full <- extract_stats_with_ci(m.full)

variable <- c("intrcpt", "performance", "procedural", 
              "year ", "round",
              "organization", "egypt", "india", 
              "saudiarabia", "unitedkingdom", "length")  

stats_full$Name <- variable



## Table 2 (column 1)
write.csv(stats_all_performance, file = "table2_performance.csv", row.names = FALSE)

## Table 2 (column 2)
write.csv(stats_all_procedural, file = "table_2_procedural.csv", row.names = FALSE)

## Table 2 (column 3)
write.csv(stats_all_norm, file = "table_2_mandate.csv", row.names = FALSE)

## Table 2 (column 4)
write.csv(stats_full, "table_2_combined.csv", row.names = FALSE)



## Appendix Table H.4., Table H.5., Table H.6. (Regressions)

## Load Data
Egypt = read.csv('Egypt_survey_CHN.csv')
India = read.csv('India_survey_CHN.csv')

Egypt <- Egypt %>%
  mutate(
    education = as.numeric(education_alt),
    religious_activity = as.numeric(religious_activity),
    politicalviews = as.numeric(politicalviews),
    gender = as.factor(gender),
    income = as.numeric(income),
    charitableorg = as.numeric(charitableorg)
  )

## Recode and clean
Egypt <- Egypt %>%
  filter(religious_activity != 5) %>%
  mutate(religious_activity = recode(religious_activity,
                                     `1` = 4,
                                     `2` = 3,
                                     `3` = 2,
                                     `4` = 1)) 
Egypt <- Egypt %>%
  filter(politicalviews != 6)

Egypt <- Egypt %>%
  filter(gender != 3)

Egypt <- Egypt %>%
  filter(income != 12)

Egypt <- Egypt %>%
  mutate(charitableorg = recode(charitableorg, `1` = 2, `2` = 1)) 

## Subset
Egypt_long_subset <- Egypt %>%
  filter(treatment %in% c("T7", "T9", "T11", "T13"))

Egypt_short_subset <- Egypt %>%
  filter(treatment %in% c("T8", "T10", "T12",  "T16"))

Egypt_long_subset$treatment_long1 <- factor(Egypt_long_subset$treatment, levels = c("T7", "T9", "T11", "T13")) ## 7: performance 9: procedural 11: norm (UNICEF-long)
Egypt_short_subset$treatment_short1 <- factor(Egypt_short_subset$treatment, levels = c("T8", "T10", "T12", "T16")) ## 8 performance 10 procedural 12 norm (UNICEF-short)
Egypt_long_subset$treatment_long1 <- relevel(Egypt_long_subset$treatment_long1, ref = "T13")
Egypt_short_subset$treatment_short1 <- relevel(Egypt_short_subset$treatment_short1, ref = "T16")


## Logit
model_logit_long_donate1 <- glm(win_donate_agg ~ treatment_long1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = Egypt_long_subset, family = binomial)
model_logit_short_donate1 <- glm(win_donate_agg ~ treatment_short1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = Egypt_short_subset, family = binomial)

model_logit_long_clicked1 <- glm(clicked2 ~ treatment_long1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = Egypt_long_subset, family = binomial)
model_logit_short_clicked1 <- glm(clicked2 ~ treatment_short1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = Egypt_short_subset, family = binomial)

## OLS
model_ols_long_wdonate <- lm(howmuch_donate_translated ~ treatment_long1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = Egypt_long_subset)
model_ols_short_wdonate <- lm(howmuch_donate_translated ~ treatment_short1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = Egypt_short_subset)


extract_logit_results <- function(model, model_name) {
  summary_model <- summary(model)
  coefs <- as.data.frame(summary_model$coefficients)
  
  colnames(coefs) <- c("Estimate", "Std. Error", "z-value", "p-value")
  
  conf_int <- as.data.frame(confint(model))
  colnames(conf_int) <- c("CI_Lower", "CI_Upper")
  
  results <- cbind(Model = model_name, Variable = rownames(coefs), coefs, conf_int)
  
  N <- nobs(model)  
  AIC_value <- AIC(model)
  BIC_value <- BIC(model)
  pseudo_r2 <- 1 - (model$deviance / model$null.deviance) 
  
  model_stats <- data.frame(
    Model = model_name, 
    N = N, 
    AIC = AIC_value, 
    BIC = BIC_value, 
    Pseudo_R2 = pseudo_r2
  )
  
  return(list(results = results, stats = model_stats))
}


extract_ols_results <- function(model, model_name) {
  summary_model <- summary(model)
  coefs <- as.data.frame(summary_model$coefficients)
  
  colnames(coefs) <- c("Estimate", "Std. Error", "t-value", "p-value")
  
  conf_int <- as.data.frame(confint(model))
  colnames(conf_int) <- c("CI_Lower", "CI_Upper")
  
  results <- cbind(Model = model_name, Variable = rownames(coefs), coefs, conf_int)

  N <- nobs(model)
  R2 <- summary_model$r.squared
  Adj_R2 <- summary_model$adj.r.squared
  AIC_value <- AIC(model)
  BIC_value <- BIC(model)
  
  model_stats <- data.frame(
    Model = model_name, 
    N = N, 
    AIC = AIC_value, 
    BIC = BIC_value, 
    R2 = R2, 
    Adj_R2 = Adj_R2
  )
  
  return(list(results = results, stats = model_stats))
}


# Define logit models
logit_models <- list(
  model_logit_long_donate1 = model_logit_long_donate1,
  model_logit_short_donate1 = model_logit_short_donate1,
  model_logit_long_clicked1 = model_logit_long_clicked1,
  model_logit_short_clicked1 = model_logit_short_clicked1)

# Define OLS models
ols_models <- list(
  model_ols_long_wdonate = model_ols_long_wdonate,
  model_ols_short_wdonate = model_ols_short_wdonate)

# Extract results for logit models
logit_results_list <- lapply(names(logit_models), function(name) {
  extract_logit_results(logit_models[[name]], name)
})

# Extract results for OLS models
ols_results_list <- lapply(names(ols_models), function(name) {
  extract_ols_results(ols_models[[name]], name)
})

# Combine results for logit models
logit_coef_results <- do.call(rbind, lapply(logit_results_list, function(x) x$results))
logit_model_stats <- do.call(rbind, lapply(logit_results_list, function(x) x$stats))

# Combine results for OLS models
ols_coef_results <- do.call(rbind, lapply(ols_results_list, function(x) x$results))
ols_model_stats <- do.call(rbind, lapply(ols_results_list, function(x) x$stats))


round_df <- function(df, digits) {
  df[] <- lapply(df, function(x) if(is.numeric(x)) round(x, digits) else x)
  return(df)
}

logit_coef_results <- round_df(logit_coef_results, 3)
logit_model_stats  <- round_df(logit_model_stats, 3)
ols_coef_results   <- round_df(ols_coef_results, 3)
ols_model_stats    <- round_df(ols_model_stats, 3)


# Appendix Table H.4., H.5.
write.csv(logit_coef_results, "appendix_table_h4_h5_coef_egypt.csv", row.names = FALSE)
write.csv(logit_model_stats, "appendix_table_h4_h5_modelstats_egypt.csv", row.names = FALSE)

# Appendix Table H.6.
write.csv(ols_coef_results, "appendix_table_h6_coef_egypt.csv", row.names = FALSE)
write.csv(ols_model_stats, "appendix_table_h6_modelstats_egypt.csv", row.names = FALSE)


## India
India <- India %>%
  mutate(
    education = as.numeric(education_alt),
    religious_activity = as.numeric(religious_activity),
    politicalviews = as.numeric(politicalviews),
    gender = as.factor(gender),
    income = as.numeric(income),
    charitableorg = as.numeric(charitableorg)
  )

## Recode and clean
India <- India %>%
  filter(religious_activity != 5) %>%
  mutate(religious_activity = recode(religious_activity,
                                     `1` = 4,
                                     `2` = 3,
                                     `3` = 2,
                                     `4` = 1)) 
India <- India %>%
  filter(politicalviews != 6)

India <- India %>%
  filter(gender != 3)

India <- India %>%
  filter(income != 12)

India <- India %>%
  mutate(charitableorg = recode(charitableorg, `1` = 2, `2` = 1)) 

## Subset
India_long_subset <- India %>%
  filter(treatment %in% c("T7", "T9", "T11", "T13"))

India_short_subset <- India %>%
  filter(treatment %in% c("T8", "T10", "T12",  "T16"))

India_long_subset$treatment_long1 <- factor(India_long_subset$treatment, levels = c("T7", "T9", "T11", "T13")) ## 7: performance 9: procedural 11: norm (UNICEF-long)
India_short_subset$treatment_short1 <- factor(India_short_subset$treatment, levels = c("T8", "T10", "T12", "T16")) ## 8 performance 10 procedural 12 norm (UNICEF-short)
India_long_subset$treatment_long1 <- relevel(India_long_subset$treatment_long1, ref = "T13")
India_short_subset$treatment_short1 <- relevel(India_short_subset$treatment_short1, ref = "T16")


## Logit
model_logit_long_donate1 <- glm(win_donate_agg ~ treatment_long1 + gender + education + income + religious_activity + politicalviews + charitableorg, data = India_long_subset, family = binomial)
model_logit_short_donate1 <- glm(win_donate_agg ~ treatment_short1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = India_short_subset, family = binomial)

model_logit_long_clicked1 <- glm(clicked2 ~ treatment_long1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = India_long_subset, family = binomial)
model_logit_short_clicked1 <- glm(clicked2 ~ treatment_short1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = India_short_subset, family = binomial)

## OLS
model_ols_long_wdonate <- lm(howmuch_donate_translated~ treatment_long1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = India_long_subset)
model_ols_short_wdonate <- lm(howmuch_donate_translated ~ treatment_short1 + gender + education + income + religious_activity  + politicalviews + charitableorg, data = India_short_subset)


extract_logit_results <- function(model, model_name) {
  summary_model <- summary(model)
  coefs <- as.data.frame(summary_model$coefficients)
  
  colnames(coefs) <- c("Estimate", "Std. Error", "z-value", "p-value")
  
  conf_int <- as.data.frame(confint(model))
  colnames(conf_int) <- c("CI_Lower", "CI_Upper")
  
  results <- cbind(Model = model_name, Variable = rownames(coefs), coefs, conf_int)
  
  N <- nobs(model)  
  AIC_value <- AIC(model)
  BIC_value <- BIC(model)
  pseudo_r2 <- 1 - (model$deviance / model$null.deviance)  
  
  model_stats <- data.frame(
    Model = model_name, 
    N = N, 
    AIC = AIC_value, 
    BIC = BIC_value, 
    Pseudo_R2 = pseudo_r2
  )
  
  return(list(results = results, stats = model_stats))
}


extract_ols_results <- function(model, model_name) {
  summary_model <- summary(model)
  coefs <- as.data.frame(summary_model$coefficients)
  
  colnames(coefs) <- c("Estimate", "Std. Error", "t-value", "p-value")
  
  conf_int <- as.data.frame(confint(model))
  colnames(conf_int) <- c("CI_Lower", "CI_Upper")
  
  results <- cbind(Model = model_name, Variable = rownames(coefs), coefs, conf_int)
  
  N <- nobs(model)  
  R2 <- summary_model$r.squared
  Adj_R2 <- summary_model$adj.r.squared
  AIC_value <- AIC(model)
  BIC_value <- BIC(model)
  
  model_stats <- data.frame(
    Model = model_name, 
    N = N, 
    AIC = AIC_value, 
    BIC = BIC_value, 
    R2 = R2, 
    Adj_R2 = Adj_R2
  )
  
  return(list(results = results, stats = model_stats))
}


# Define logit models
logit_models <- list(
  model_logit_long_donate1 = model_logit_long_donate1,
  model_logit_short_donate1 = model_logit_short_donate1,
  model_logit_long_clicked1 = model_logit_long_clicked1,
  model_logit_short_clicked1 = model_logit_short_clicked1
)

# Define OLS models
ols_models <- list(
  model_ols_long_wdonate = model_ols_long_wdonate,
  model_ols_short_wdonate = model_ols_short_wdonate
)

# Extract results for logit models
logit_results_list <- lapply(names(logit_models), function(name) {
  extract_logit_results(logit_models[[name]], name)
})

# Extract results for OLS models
ols_results_list <- lapply(names(ols_models), function(name) {
  extract_ols_results(ols_models[[name]], name)
})

# Combine results for logit models
logit_coef_results <- do.call(rbind, lapply(logit_results_list, function(x) x$results))
logit_model_stats <- do.call(rbind, lapply(logit_results_list, function(x) x$stats))

# Combine results for OLS models
ols_coef_results <- do.call(rbind, lapply(ols_results_list, function(x) x$results))
ols_model_stats <- do.call(rbind, lapply(ols_results_list, function(x) x$stats))


round_df <- function(df, digits) {
  df[] <- lapply(df, function(x) if(is.numeric(x)) round(x, digits) else x)
  return(df)
}

logit_coef_results <- round_df(logit_coef_results, 3)
logit_model_stats  <- round_df(logit_model_stats, 3)
ols_coef_results   <- round_df(ols_coef_results, 3)
ols_model_stats    <- round_df(ols_model_stats, 3)


# Appendix Table H.4., H.5.
write.csv(logit_coef_results, "appendix_table_h4_h5_coef_india.csv", row.names = FALSE)
write.csv(logit_model_stats, "appendix_table_h4_h5_modelstats_india.csv", row.names = FALSE)

# Appendix Table H.6.
write.csv(ols_coef_results, "appendix_table_h6_coef_india.csv", row.names = FALSE)
write.csv(ols_model_stats, "appendix_table_h6_modelstats_india.csv", row.names = FALSE)



# Appendix C. (Randomization Inference)

## Load package
library(ri2)

## set seed
set.seed(2025)


# Egypt

## Donate (intention)
Egypt_T13=as.data.frame(Egypt_T13)
Egypt_T13$Y <- Egypt_T13$win_donate_agg
Egypt_T13$Z=0
Egypt_T13$win_donate_agg=NULL

Egypt_T7=as.data.frame(Egypt_T7)
Egypt_T7$Y=Egypt_T7$win_donate_agg
Egypt_T7$Z=1
Egypt_T7$win_donate_agg=NULL


Egypt_T9=as.data.frame(Egypt_T9)
Egypt_T9$Y=Egypt_T9$win_donate_agg
Egypt_T9$Z=1
Egypt_T9$win_donate_agg=NULL

Egypt_T11=as.data.frame(Egypt_T11)
Egypt_T11$Y=Egypt_T11$win_donate_agg
Egypt_T11$Z=1
Egypt_T11$win_donate_agg=NULL

Egypt_T10=as.data.frame(Egypt_T10)
Egypt_T10$Y=Egypt_T10$win_donate_agg
Egypt_T10$Z=1
Egypt_T10$win_donate_agg=NULL

Egypt_T12=as.data.frame(Egypt_T12)
Egypt_T12$Y=Egypt_T12$win_donate_agg
Egypt_T12$Z=1
Egypt_T12$win_donate_agg=NULL

Egypt_T8=as.data.frame(Egypt_T8)
Egypt_T8$Y=Egypt_T8$win_donate_agg
Egypt_T8$Z=1
Egypt_T8$win_donate_agg=NULL

Egypt_T16=as.data.frame(Egypt_T16)
Egypt_T16$Y=Egypt_T16$win_donate_agg
Egypt_T16$Z=0
Egypt_T16$win_donate_agg=NULL


# Create dataframe and declare RI
Egypt_T7_data=rbind(Egypt_T13, Egypt_T7)
table(Egypt_T7_data)

Egypt_T7_data <- Egypt_T7_data[!is.na(Egypt_T7_data$Y), ]

Egypt_T7_data1 <- declare_ra(
  N = nrow(Egypt_T7_data), 
  m = sum(Egypt_T7_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T7_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T7_data1,
  sharp_hypothesis = 0,
  data = Egypt_T7_data
)


# Create dataframe and declare RI
Egypt_T9_data=rbind(Egypt_T13, Egypt_T9)
table(Egypt_T9_data)

Egypt_T9_data <- Egypt_T9_data[!is.na(Egypt_T9_data$Y), ]

Egypt_T9_data1 <- declare_ra(
  N = nrow(Egypt_T9_data), 
  m = sum(Egypt_T9_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T9_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T9_data1,
  sharp_hypothesis = 0,
  data = Egypt_T9_data
)


# Create dataframe and declare RI
Egypt_T11_data=rbind(Egypt_T13, Egypt_T11)
table(Egypt_T11_data)

Egypt_T11_data <- Egypt_T11_data[!is.na(Egypt_T11_data$Y), ]

Egypt_T11_data1 <- declare_ra(
  N = nrow(Egypt_T11_data), 
  m = sum(Egypt_T11_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T11_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T11_data1,
  sharp_hypothesis = 0,
  data = Egypt_T11_data
)


# Create dataframe and declare RI
Egypt_T10_data=rbind(Egypt_T16, Egypt_T10)
table(Egypt_T10_data)

Egypt_T10_data <- Egypt_T10_data[!is.na(Egypt_T10_data$Y), ]

Egypt_T10_data1 <- declare_ra(
  N = nrow(Egypt_T10_data), 
  m = sum(Egypt_T10_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T10_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T10_data1,
  sharp_hypothesis = 0,
  data = Egypt_T10_data
)


# Create dataframes and declare RI
Egypt_T12_data=rbind(Egypt_T16, Egypt_T12)
table(Egypt_T12_data)

Egypt_T12_data <- Egypt_T12_data[!is.na(Egypt_T12_data$Y), ]

Egypt_T12_data1 <- declare_ra(
  N = nrow(Egypt_T12_data), 
  m = sum(Egypt_T12_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T12_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T12_data1,
  sharp_hypothesis = 0,
  data = Egypt_T12_data
)


# Create dataframes and declare RI
Egypt_T8_data=rbind(Egypt_T16, Egypt_T8)
table(Egypt_T8_data)

Egypt_T8_data <- Egypt_T8_data[!is.na(Egypt_T8_data$Y), ]

Egypt_T8_data1 <- declare_ra(
  N = nrow(Egypt_T8_data), 
  m = sum(Egypt_T8_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T8_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T8_data1,
  sharp_hypothesis = 0,
  data = Egypt_T8_data
)


## Clicks
Egypt_T13c=as.data.frame(Egypt_T13c)
Egypt_T13c$Y <- Egypt_T13c$clicked2
Egypt_T13c$Z=0
Egypt_T13c$clicked2=NULL

Egypt_T7c=as.data.frame(Egypt_T7c)
Egypt_T7c$Y=Egypt_T7c$clicked2
Egypt_T7c$Z=1
Egypt_T7c$clicked2=NULL

Egypt_T9c=as.data.frame(Egypt_T9c)
Egypt_T9c$Y=Egypt_T9c$clicked2
Egypt_T9c$Z=1
Egypt_T9c$clicked2=NULL

Egypt_T11c=as.data.frame(Egypt_T11c)
Egypt_T11c$Y=Egypt_T11c$clicked2
Egypt_T11c$Z=1
Egypt_T11c$clicked2=NULL

Egypt_T10c=as.data.frame(Egypt_T10c)
Egypt_T10c$Y=Egypt_T10c$clicked2
Egypt_T10c$Z=1
Egypt_T10c$clicked2=NULL

Egypt_T12c=as.data.frame(Egypt_T12c)
Egypt_T12c$Y=Egypt_T12c$clicked2
Egypt_T12c$Z=1
Egypt_T12c$clicked2=NULL

Egypt_T8c=as.data.frame(Egypt_T8c)
Egypt_T8c$Y=Egypt_T8c$clicked2
Egypt_T8c$Z=1
Egypt_T8c$clicked2=NULL

Egypt_T16c=as.data.frame(Egypt_T16c)
Egypt_T16c$Y=Egypt_T16c$clicked2
Egypt_T16c$Z=0
Egypt_T16c$clicked2=NULL


# Create dataframe and declare RI
Egypt_T7c_data=rbind(Egypt_T13c, Egypt_T7c)
table(Egypt_T7c_data)

Egypt_T7c_data <- Egypt_T7c_data[!is.na(Egypt_T7c_data$Y), ]

Egypt_T7c_data1 <- declare_ra(
  N = nrow(Egypt_T7c_data), 
  m = sum(Egypt_T7c_data$Z)
)

# Conduct randomization inference
ri2_Egypt_T7c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T7c_data1,
  sharp_hypothesis = 0,
  data = Egypt_T7c_data
)


# Create dataframe and declare RI
Egypt_T9c_data=rbind(Egypt_T13c, Egypt_T9c)
table(Egypt_T9c_data)

Egypt_T9c_data <- Egypt_T9c_data[!is.na(Egypt_T9c_data$Y), ]

Egypt_T9c_data1 <- declare_ra(
  N = nrow(Egypt_T9c_data), 
  m = sum(Egypt_T9c_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T9c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T9c_data1,
  sharp_hypothesis = 0,
  data = Egypt_T9c_data
)


# Create dataframe and declare RI
Egypt_T11c_data=rbind(Egypt_T13c, Egypt_T11c)
table(Egypt_T11c_data)

Egypt_T11c_data <- Egypt_T11c_data[!is.na(Egypt_T11c_data$Y), ]

Egypt_T11c_data1 <- declare_ra(
  N = nrow(Egypt_T11c_data), 
  m = sum(Egypt_T11c_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T11c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T11c_data1,
  sharp_hypothesis = 0,
  data = Egypt_T11c_data
)


# Create dataframe and declare RI
Egypt_T10c_data=rbind(Egypt_T16c, Egypt_T10c)
table(Egypt_T10c_data)

Egypt_T10c_data <- Egypt_T10c_data[!is.na(Egypt_T10c_data$Y), ]

Egypt_T10c_data1 <- declare_ra(
  N = nrow(Egypt_T10c_data), 
  m = sum(Egypt_T10c_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T10c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T10c_data1,
  sharp_hypothesis = 0,
  data = Egypt_T10c_data
)


# Create dataframe and declare RI
Egypt_T12c_data=rbind(Egypt_T16c, Egypt_T12c)
table(Egypt_T12c_data)

Egypt_T12c_data <- Egypt_T12c_data[!is.na(Egypt_T12c_data$Y), ]

Egypt_T12c_data1 <- declare_ra(
  N = nrow(Egypt_T12c_data), 
  m = sum(Egypt_T12c_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T12c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T12c_data1,
  sharp_hypothesis = 0,
  data = Egypt_T12c_data
)


# Create dataframe and declare RI
Egypt_T8c_data=rbind(Egypt_T16c, Egypt_T8c)
table(Egypt_T8c_data)

Egypt_T8c_data <- Egypt_T8c_data[!is.na(Egypt_T8c_data$Y), ]

Egypt_T8c_data1 <- declare_ra(
  N = nrow(Egypt_T8c_data), 
  m = sum(Egypt_T8c_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T8c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T8c_data1,
  sharp_hypothesis = 0,
  data = Egypt_T8c_data
)


## Winnings
Egypt_T13w=as.data.frame(Egypt_T13w)
Egypt_T13w$Y <- Egypt_T13w$howmuch_donate_translated
Egypt_T13w$Z=0
Egypt_T13w$howmuch_donate_translated=NULL

Egypt_T7w=as.data.frame(Egypt_T7w)
Egypt_T7w$Y=Egypt_T7w$howmuch_donate_translated
Egypt_T7w$Z=1
Egypt_T7w$howmuch_donate_translated=NULL

Egypt_T9w=as.data.frame(Egypt_T9w)
Egypt_T9w$Y=Egypt_T9w$howmuch_donate_translated
Egypt_T9w$Z=1
Egypt_T9w$howmuch_donate_translated=NULL

Egypt_T11w=as.data.frame(Egypt_T11w)
Egypt_T11w$Y=Egypt_T11w$howmuch_donate_translated
Egypt_T11w$Z=1
Egypt_T11w$howmuch_donate_translated=NULL

Egypt_T10w=as.data.frame(Egypt_T10w)
Egypt_T10w$Y=Egypt_T10w$howmuch_donate_translated
Egypt_T10w$Z=1
Egypt_T10w$howmuch_donate_translated=NULL

Egypt_T12w=as.data.frame(Egypt_T12w)
Egypt_T12w$Y=Egypt_T12w$howmuch_donate_translated
Egypt_T12w$Z=1
Egypt_T12w$howmuch_donate_translated=NULL

Egypt_T8w=as.data.frame(Egypt_T8w)
Egypt_T8w$Y=Egypt_T8w$howmuch_donate_translated
Egypt_T8w$Z=1
Egypt_T8w$howmuch_donate_translated=NULL

Egypt_T16w=as.data.frame(Egypt_T16w)
Egypt_T16w$Y=Egypt_T16w$howmuch_donate_translated
Egypt_T16w$Z=0
Egypt_T16w$howmuch_donate_translated=NULL


# Create dataframe and declare RI
Egypt_T7w_data=rbind(Egypt_T13w, Egypt_T7w)
table(Egypt_T7w_data)

Egypt_T7w_data <- Egypt_T7w_data[!is.na(Egypt_T7w_data$Y), ]

Egypt_T7w_data1 <- declare_ra(
  N = nrow(Egypt_T7w_data), 
  m = sum(Egypt_T7w_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T7w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T7w_data1,
  sharp_hypothesis = 0,
  data = Egypt_T7w_data
)


# Create dataframes and declare RI
Egypt_T9w_data=rbind(Egypt_T13w, Egypt_T9w)
table(Egypt_T9w_data)

Egypt_T9w_data <- Egypt_T9w_data[!is.na(Egypt_T9w_data$Y), ]

Egypt_T9w_data1 <- declare_ra(
  N = nrow(Egypt_T9w_data), 
  m = sum(Egypt_T9w_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T9w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T9w_data1,
  sharp_hypothesis = 0,
  data = Egypt_T9w_data
)


# Create dataframe and declare RI
Egypt_T11w_data=rbind(Egypt_T13w, Egypt_T11w)
table(Egypt_T11w_data)

Egypt_T11w_data <- Egypt_T11w_data[!is.na(Egypt_T11w_data$Y), ]

Egypt_T11w_data1 <- declare_ra(
  N = nrow(Egypt_T11w_data), 
  m = sum(Egypt_T11w_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T11w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T11w_data1,
  sharp_hypothesis = 0,
  data = Egypt_T11w_data
)


# Create dataframe and declare RI
Egypt_T10w_data=rbind(Egypt_T16w, Egypt_T10w)
table(Egypt_T10w_data)

Egypt_T10w_data <- Egypt_T10w_data[!is.na(Egypt_T10w_data$Y), ]

Egypt_T10w_data1 <- declare_ra(
  N = nrow(Egypt_T10w_data), 
  m = sum(Egypt_T10w_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T10w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T10w_data1,
  sharp_hypothesis = 0,
  data = Egypt_T10w_data
)


# Create dataframe and declare RI
Egypt_T12w_data=rbind(Egypt_T16w, Egypt_T12w)
table(Egypt_T12w_data)

Egypt_T12w_data <- Egypt_T12w_data[!is.na(Egypt_T12w_data$Y), ]

Egypt_T12w_data1 <- declare_ra(
  N = nrow(Egypt_T12w_data), 
  m = sum(Egypt_T12w_data$Z)
)

# Conduct randomization inference 
ri2_Egypt_T12w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T12w_data1,
  sharp_hypothesis = 0,
  data = Egypt_T12w_data
)


# Create dataframe and declare RI
Egypt_T8w_data=rbind(Egypt_T16w, Egypt_T8w)
table(Egypt_T8w_data)

Egypt_T8w_data <- Egypt_T8w_data[!is.na(Egypt_T8w_data$Y), ]

Egypt_T8w_data1 <- declare_ra(
  N = nrow(Egypt_T8w_data), 
  m = sum(Egypt_T8w_data$Z)
)

# Conduct randomization inference
ri2_Egypt_T8w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = Egypt_T8w_data1,
  sharp_hypothesis = 0,
  data = Egypt_T8w_data
)


## India

## Donate (intention)
India_T13=as.data.frame(India_T13)
India_T13$Y <- India_T13$win_donate_agg
India_T13$Z=0
India_T13$win_donate_agg=NULL

India_T7=as.data.frame(India_T7)
India_T7$Y=India_T7$win_donate_agg
India_T7$Z=1
India_T7$win_donate_agg=NULL

India_T9=as.data.frame(India_T9)
India_T9$Y=India_T9$win_donate_agg
India_T9$Z=1
India_T9$win_donate_agg=NULL

India_T11=as.data.frame(India_T11)
India_T11$Y=India_T11$win_donate_agg
India_T11$Z=1
India_T11$win_donate_agg=NULL

India_T10=as.data.frame(India_T10)
India_T10$Y=India_T10$win_donate_agg
India_T10$Z=1
India_T10$win_donate_agg=NULL

India_T12=as.data.frame(India_T12)
India_T12$Y=India_T12$win_donate_agg
India_T12$Z=1
India_T12$win_donate_agg=NULL

India_T8=as.data.frame(India_T8)
India_T8$Y=India_T8$win_donate_agg
India_T8$Z=1
India_T8$win_donate_agg=NULL

India_T16=as.data.frame(India_T16)
India_T16$Y=India_T16$win_donate_agg
India_T16$Z=0
India_T16$win_donate_agg=NULL


# Create dataframe and declare RI
India_T7_data=rbind(India_T13, India_T7)
table(India_T7_data)

India_T7_data <- India_T7_data[!is.na(India_T7_data$Y), ]

India_T7_data1 <- declare_ra(
  N = nrow(India_T7_data), 
  m = sum(India_T7_data$Z)
)

# Conduct randomization inference 
ri2_India_T7_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T7_data1,
  sharp_hypothesis = 0,
  data = India_T7_data
)


# Create dataframe and declare RI
India_T9_data=rbind(India_T13, India_T9)
table(India_T9_data)

India_T9_data <- India_T9_data[!is.na(India_T9_data$Y), ]

India_T9_data1 <- declare_ra(
  N = nrow(India_T9_data), 
  m = sum(India_T9_data$Z)
)

# Conduct randomization inference 
ri2_India_T9_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T9_data1,
  sharp_hypothesis = 0,
  data = India_T9_data
)


# Create dataframe and declare RI
India_T11_data=rbind(India_T13, India_T11)
table(India_T11_data)

India_T11_data <- India_T11_data[!is.na(India_T11_data$Y), ]

India_T11_data1 <- declare_ra(
  N = nrow(India_T11_data), 
  m = sum(India_T11_data$Z)
)

# Conduct randomization inference 
ri2_India_T11_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T11_data1,
  sharp_hypothesis = 0,
  data = India_T11_data
)


# Create dataframe and declare RI
India_T10_data=rbind(India_T16, India_T10)
table(India_T10_data)

India_T10_data <- India_T10_data[!is.na(India_T10_data$Y), ]

India_T10_data1 <- declare_ra(
  N = nrow(India_T10_data), 
  m = sum(India_T10_data$Z)
)

# Conduct randomization inference 
ri2_India_T10_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T10_data1,
  sharp_hypothesis = 0,
  data = India_T10_data
)


# Create dataframe and declare RI
India_T12_data=rbind(India_T16, India_T12)
table(India_T12_data)

India_T12_data <- India_T12_data[!is.na(India_T12_data$Y), ]

India_T12_data1 <- declare_ra(
  N = nrow(India_T12_data), 
  m = sum(India_T12_data$Z)
)

# Conduct randomization inference 
ri2_India_T12_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T12_data1,
  sharp_hypothesis = 0,
  data = India_T12_data
)


# Create dataframe and declare RI
India_T8_data=rbind(India_T16, India_T8)
table(India_T8_data)

India_T8_data <- India_T8_data[!is.na(India_T8_data$Y), ]

India_T8_data1 <- declare_ra(
  N = nrow(India_T8_data), 
  m = sum(India_T8_data$Z)
)

# Conduct randomization inference
ri2_India_T8_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T8_data1,
  sharp_hypothesis = 0,
  data = India_T8_data
)


## Clicks
India_T13c=as.data.frame(India_T13c)
India_T13c$Y <- India_T13c$clicked2
India_T13c$Z=0
India_T13c$clicked2=NULL

India_T7c=as.data.frame(India_T7c)
India_T7c$Y=India_T7c$clicked2
India_T7c$Z=1
India_T7c$clicked2=NULL

India_T9c=as.data.frame(India_T9c)
India_T9c$Y=India_T9c$clicked2
India_T9c$Z=1
India_T9c$clicked2=NULL

India_T11c=as.data.frame(India_T11c)
India_T11c$Y=India_T11c$clicked2
India_T11c$Z=1
India_T11c$clicked2=NULL

India_T10c=as.data.frame(India_T10c)
India_T10c$Y=India_T10c$clicked2
India_T10c$Z=1
India_T10c$clicked2=NULL

India_T12c=as.data.frame(India_T12c)
India_T12c$Y=India_T12c$clicked2
India_T12c$Z=1
India_T12c$clicked2=NULL

India_T8c=as.data.frame(India_T8c)
India_T8c$Y=India_T8c$clicked2
India_T8c$Z=1
India_T8c$clicked2=NULL

India_T16c=as.data.frame(India_T16c)
India_T16c$Y=India_T16c$clicked2
India_T16c$Z=0
India_T16c$clicked2=NULL


# Create dataframe and declare RI
India_T7c_data=rbind(India_T13c, India_T7c)
table(India_T7c_data)

India_T7c_data <- India_T7c_data[!is.na(India_T7c_data$Y), ]

India_T7c_data1 <- declare_ra(
  N = nrow(India_T7c_data), 
  m = sum(India_T7c_data$Z)
)

# Conduct randomization inference 
ri2_India_T7c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T7c_data1,
  sharp_hypothesis = 0,
  data = India_T7c_data
)


# Create dataframe and declare RI
India_T9c_data=rbind(India_T13c, India_T9c)
table(India_T9c_data)

India_T9c_data <- India_T9c_data[!is.na(India_T9c_data$Y), ]

India_T9c_data1 <- declare_ra(
  N = nrow(India_T9c_data), 
  m = sum(India_T9c_data$Z)
)

# Conduct randomization inference 
ri2_India_T9c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T9c_data1,
  sharp_hypothesis = 0,
  data = India_T9c_data
)


# Create dataframe and declare RI
India_T11c_data=rbind(India_T13c, India_T11c)
table(India_T11c_data)

India_T11c_data <- India_T11c_data[!is.na(India_T11c_data$Y), ]

India_T11c_data1 <- declare_ra(
  N = nrow(India_T11c_data), 
  m = sum(India_T11c_data$Z)
)

# Conduct randomization inference
ri2_India_T11c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T11c_data1,
  sharp_hypothesis = 0,
  data = India_T11c_data
)


# Create dataframe and declare RI
India_T10c_data=rbind(India_T16c, India_T10c)
table(India_T10c_data)

India_T10c_data <- India_T10c_data[!is.na(India_T10c_data$Y), ]

India_T10c_data1 <- declare_ra(
  N = nrow(India_T10c_data), 
  m = sum(India_T10c_data$Z)
)

# Conduct randomization inference 
ri2_India_T10c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T10c_data1,
  sharp_hypothesis = 0,
  data = India_T10c_data
)


# Create dataframe and declare RI
India_T12c_data=rbind(India_T16c, India_T12c)
table(India_T12c_data)

India_T12c_data <- India_T12c_data[!is.na(India_T12c_data$Y), ]

India_T12c_data1 <- declare_ra(
  N = nrow(India_T12c_data), 
  m = sum(India_T12c_data$Z)
)

# Conduct randomization inference 
ri2_India_T12c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T12c_data1,
  sharp_hypothesis = 0,
  data = India_T12c_data
)


# Create dataframe and declare RI
India_T8c_data=rbind(India_T16c, India_T8c)
table(India_T8c_data)

India_T8c_data <- India_T8c_data[!is.na(India_T8c_data$Y), ]

India_T8c_data1 <- declare_ra(
  N = nrow(India_T8c_data), 
  m = sum(India_T8c_data$Z)
)

# Conduct randomization inference 
ri2_India_T8c_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T8c_data1,
  sharp_hypothesis = 0,
  data = India_T8c_data
)


## Winnings
India_T13w=as.data.frame(India_T13w)
India_T13w$Y <- India_T13w$howmuch_donate_translated
India_T13w$Z=0
India_T13w$howmuch_donate_translated=NULL

India_T7w=as.data.frame(India_T7w)
India_T7w$Y=India_T7w$howmuch_donate_translated
India_T7w$Z=1
India_T7w$howmuch_donate_translated=NULL

India_T9w=as.data.frame(India_T9w)
India_T9w$Y=India_T9w$howmuch_donate_translated
India_T9w$Z=1
India_T9w$howmuch_donate_translated=NULL

India_T11w=as.data.frame(India_T11w)
India_T11w$Y=India_T11w$howmuch_donate_translated
India_T11w$Z=1
India_T11w$howmuch_donate_translated=NULL

India_T10w=as.data.frame(India_T10w)
India_T10w$Y=India_T10w$howmuch_donate_translated
India_T10w$Z=1
India_T10w$howmuch_donate_translated=NULL

India_T12w=as.data.frame(India_T12w)
India_T12w$Y=India_T12w$howmuch_donate_translated
India_T12w$Z=1
India_T12w$howmuch_donate_translated=NULL

India_T8w=as.data.frame(India_T8w)
India_T8w$Y=India_T8w$howmuch_donate_translated
India_T8w$Z=1
India_T8w$howmuch_donate_translated=NULL

India_T16w=as.data.frame(India_T16w)
India_T16w$Y=India_T16w$howmuch_donate_translated
India_T16w$Z=0
India_T16w$howmuch_donate_translated=NULL


# Create dataframe and declare RI
India_T7w_data=rbind(India_T13w, India_T7w)
table(India_T7w_data)

India_T7w_data <- India_T7w_data[!is.na(India_T7w_data$Y), ]

India_T7w_data1 <- declare_ra(
  N = nrow(India_T7w_data), 
  m = sum(India_T7w_data$Z)
)

# Conduct randomization inference
ri2_India_T7w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T7w_data1,
  sharp_hypothesis = 0,
  data = India_T7w_data
)


# Create dataframe and declare RI
India_T9w_data=rbind(India_T13w, India_T9w)
table(India_T9w_data)

India_T9w_data <- India_T9w_data[!is.na(India_T9w_data$Y), ]

India_T9w_data1 <- declare_ra(
  N = nrow(India_T9w_data), 
  m = sum(India_T9w_data$Z)
)

# Conduct randomization inference 
ri2_India_T9w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T9w_data1,
  sharp_hypothesis = 0,
  data = India_T9w_data
)


# Create dataframe and declare RI
India_T11w_data=rbind(India_T13w, India_T11w)
table(India_T11w_data)

India_T11w_data <- India_T11w_data[!is.na(India_T11w_data$Y), ]

India_T11w_data1 <- declare_ra(
  N = nrow(India_T11w_data), 
  m = sum(India_T11w_data$Z)
)

# Conduct randomization inference
ri2_India_T11w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T11w_data1,
  sharp_hypothesis = 0,
  data = India_T11w_data
)


# Create dataframe and declare RI
India_T10w_data=rbind(India_T16w, India_T10w)
table(India_T10w_data)

India_T10w_data <- India_T10w_data[!is.na(India_T10w_data$Y), ]

India_T10w_data1 <- declare_ra(
  N = nrow(India_T10w_data), 
  m = sum(India_T10w_data$Z)
)
 
# Conduct randomization inference 
ri2_India_T10w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T10w_data1,
  sharp_hypothesis = 0,
  data = India_T10w_data
)


# Create dataframe and declare RI
India_T12w_data=rbind(India_T16w, India_T12w)
table(India_T12w_data)

India_T12w_data <- India_T12w_data[!is.na(India_T12w_data$Y), ]

India_T12w_data1 <- declare_ra(
  N = nrow(India_T12w_data), 
  m = sum(India_T12w_data$Z)
)

# Conduct randomization inference
ri2_India_T12w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T12w_data1,
  sharp_hypothesis = 0,
  data = India_T12w_data
)


# Create dataframe and declare RI
India_T8w_data=rbind(India_T16w, India_T8w)
table(India_T8w_data)

India_T8w_data <- India_T8w_data[!is.na(India_T8w_data$Y), ]

India_T8w_data1 <- declare_ra(
  N = nrow(India_T8w_data), 
  m = sum(India_T8w_data$Z)
)

# Conduct randomization inference 
ri2_India_T8w_data <- conduct_ri(
  formula = Y ~ Z,
  declaration = India_T8w_data1,
  sharp_hypothesis = 0,
  data = India_T8w_data
)



## RI plots
plot(ri2_Egypt_T7_data)
plot(ri2_Egypt_T9_data)
plot(ri2_Egypt_T11_data)
plot(ri2_Egypt_T10_data)
plot(ri2_Egypt_T12_data)
plot(ri2_Egypt_T8_data)

plot(ri2_Egypt_T7c_data)
plot(ri2_Egypt_T9c_data)
plot(ri2_Egypt_T11c_data)
plot(ri2_Egypt_T10c_data)
plot(ri2_Egypt_T12c_data)
plot(ri2_Egypt_T8c_data)

plot(ri2_Egypt_T7w_data)
plot(ri2_Egypt_T9w_data)
plot(ri2_Egypt_T11w_data)
plot(ri2_Egypt_T10w_data)
plot(ri2_Egypt_T12w_data)
plot(ri2_Egypt_T8w_data)

plot(ri2_India_T7_data)
plot(ri2_India_T9_data)
plot(ri2_India_T11_data)
plot(ri2_India_T10_data)
plot(ri2_India_T12_data)
plot(ri2_India_T8_data)

plot(ri2_India_T7c_data)
plot(ri2_India_T9c_data)
plot(ri2_India_T11c_data)
plot(ri2_India_T10c_data)
plot(ri2_India_T12c_data)
plot(ri2_India_T8c_data)

plot(ri2_India_T7w_data)
plot(ri2_India_T9w_data)
plot(ri2_India_T11w_data)
plot(ri2_India_T10w_data)
plot(ri2_India_T12w_data)
plot(ri2_India_T8w_data)


## Save plots

### India (intention)
png("plots/ri2_India_T7_data.png"); plot(ri2_India_T7_data); dev.off()
png("plots/ri2_India_T9_data.png"); plot(ri2_India_T9_data); dev.off()
png("plots/ri2_India_T11_data.png"); plot(ri2_India_T11_data); dev.off()
png("plots/ri2_India_T10_data.png"); plot(ri2_India_T10_data); dev.off()
png("plots/ri2_India_T12_data.png"); plot(ri2_India_T12_data); dev.off()
png("plots/ri2_India_T8_data.png"); plot(ri2_India_T8_data); dev.off()

### India (clicks)
png("plots/ri2_India_T7c_data.png"); plot(ri2_India_T7c_data); dev.off()
png("plots/ri2_India_T9c_data.png"); plot(ri2_India_T9c_data); dev.off()
png("plots/ri2_India_T11c_data.png"); plot(ri2_India_T11c_data); dev.off()
png("plots/ri2_India_T10c_data.png"); plot(ri2_India_T10c_data); dev.off()
png("plots/ri2_India_T12c_data.png"); plot(ri2_India_T12c_data); dev.off()
png("plots/ri2_India_T8c_data.png"); plot(ri2_India_T8c_data); dev.off()

### India (winnings)
png("plots/ri2_India_T7w_data.png"); plot(ri2_India_T7w_data); dev.off()
png("plots/ri2_India_T9w_data.png"); plot(ri2_India_T9w_data); dev.off()
png("plots/ri2_India_T11w_data.png"); plot(ri2_India_T11w_data); dev.off()
png("plots/ri2_India_T10w_data.png"); plot(ri2_India_T10w_data); dev.off()
png("plots/ri2_India_T12w_data.png"); plot(ri2_India_T12w_data); dev.off()
png("plots/ri2_India_T8w_data.png"); plot(ri2_India_T8w_data); dev.off()

### Egypt (intention)
png("plots/ri2_Egypt_T7_data.png"); plot(ri2_Egypt_T7_data); dev.off()
png("plots/ri2_Egypt_T9_data.png"); plot(ri2_Egypt_T9_data); dev.off()
png("plots/ri2_Egypt_T11_data.png"); plot(ri2_Egypt_T11_data); dev.off()
png("plots/ri2_Egypt_T10_data.png"); plot(ri2_Egypt_T10_data); dev.off()
png("plots/ri2_Egypt_T12_data.png"); plot(ri2_Egypt_T12_data); dev.off()
png("plots/ri2_Egypt_T8_data.png"); plot(ri2_Egypt_T8_data); dev.off()

### Egypt (clicks)
png("plots/ri2_Egypt_T7c_data.png"); plot(ri2_Egypt_T7c_data); dev.off()
png("plots/ri2_Egypt_T9c_data.png"); plot(ri2_Egypt_T9c_data); dev.off()
png("plots/ri2_Egypt_T11c_data.png"); plot(ri2_Egypt_T11c_data); dev.off()
png("plots/ri2_Egypt_T10c_data.png"); plot(ri2_Egypt_T10c_data); dev.off()
png("plots/ri2_Egypt_T12c_data.png"); plot(ri2_Egypt_T12c_data); dev.off()
png("plots/ri2_Egypt_T8c_data.png"); plot(ri2_Egypt_T8c_data); dev.off()

### Egypt (winnings)
png("plots/ri2_Egypt_T7w_data.png"); plot(ri2_Egypt_T7w_data); dev.off()
png("plots/ri2_Egypt_T9w_data.png"); plot(ri2_Egypt_T9w_data); dev.off()
png("plots/ri2_Egypt_T11w_data.png"); plot(ri2_Egypt_T11w_data); dev.off()
png("plots/ri2_Egypt_T10w_data.png"); plot(ri2_Egypt_T10w_data); dev.off()
png("plots/ri2_Egypt_T12w_data.png"); plot(ri2_Egypt_T12w_data); dev.off()
png("plots/ri2_Egypt_T8w_data.png"); plot(ri2_Egypt_T8w_data); dev.off()


